Vit*_* L. 6 android image save android-7.0-nougat
我想在Android Nougat上的库中打开已保存的图像,但我得到的是一个黑色的图库页面,上面写着"无法加载照片".
那是我的代码:
表现
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
Run Code Online (Sandbox Code Playgroud)
provider_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>
Run Code Online (Sandbox Code Playgroud)
DrawView中生成的路径
public static boolean save(Bitmap bitmap){
Date now = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy'_'HH:mm");
File folder = new File(Environment.getExternalStorageDirectory() +
File.separator + "Crash");
if (!folder.exists()) {
folder.mkdirs();
}
FileOutputStream fos = null;
try {
lastImagePath = new File(Environment.getExternalStorageDirectory().toString() + "/Crash/" + simpleDateFormat.format(now) + ".jpg");
fos = new FileOutputStream(lastImagePath);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
fos = null;
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {}
}
}
}
Run Code Online (Sandbox Code Playgroud)
打开图像侦听器
private class OpenImageListener implements View.OnClickListener{
@Override
public void onClick(View v) {
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.N){
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file://" + DrawView.getLastImagePath().getAbsolutePath()), "image/*");
startActivity(intent);
} else {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri photoUri = FileProvider.getUriForFile(MainActivity.this, BuildConfig.APPLICATION_ID + ".provider", DrawView.getLastImagePath());
intent.setData(photoUri);
startActivity(intent);
}
}
}
Run Code Online (Sandbox Code Playgroud)
也许我为图像生成了一条错误的路径,但是旧的版本它可以工作(我在Marshmallow上试过并且效果很好).
有人能帮我吗?谢谢.
Com*_*are 11
在你的else块onClick(),打完电话后setData()您的Intent设置Uri,调用addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)上Intent.
就目前而言,另一个应用程序无权使用该标识符所标识的内容Uri.添加FLAG_GRANT_READ_URI_PERMISSION这样做.
文档中包含此FileProvider内容,以及有关Android应用程序开发的现代书籍.
| 归档时间: |
|
| 查看次数: |
4189 次 |
| 最近记录: |