使用 Android 牛轧糖在图库中打开图片
Open image in gallery with Android Nougat
我想在 Android Nougat 的图库中打开保存的图片,但我得到的是带有消息 "Can't load the photo".
的黑色图库页面
这是我的代码:
清单
<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>
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>
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) {}
}
}
}
打开图像监听器
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);
}
}
}
也许我为图像生成了错误的路径,但旧版本可以正常工作(我试过 Marshmallow 并且效果很好)。
有人可以帮助我吗?谢谢。
我尝试过的几乎所有应用程序都会遇到同样的问题,我的代码与您的代码相同,而且 google 文档说它看起来是正确的...
我唯一可以为您提供的 "solution" 是针对版本 23(https://source.android.com/source/build-numbers.html) 进行编译以避免文件 URI 的强制严格模式。
当然,如果您正在使用它们,您还必须降级 Android 支持库。
如果有人有更好的想法,请分享...
在 onClick()
的 else
块中,在 Intent
上调用 setData()
设置 Uri
后,调用 addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
Intent
.
目前,其他应用无权使用 Uri
标识的内容。添加 FLAG_GRANT_READ_URI_PERMISSION
就是这样做的。
这在 the FileProvider
documentation 以及有关 Android 应用程序开发的现代书籍中都有介绍。
从移动模拟图库查看图像并不困难,但在牛轧糖中却大不相同,我创建了 运行 次图像,并希望看到该图像多次失败,但最终 运行使用此代码
1.after我写这段代码的长期斗争,不要忘记在 mainfest 文件中添加 fil provider,你可能知道。
MimeTypeMap map = MimeTypeMap.getSingleton();
String ext = MimeTypeMap.getFileExtensionFromUrl(file.getName());
String type = map.getMimeTypeFromExtension(ext);
if (type == null)
type = "*/*";
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// only for gingerbread and newer versions
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri photoUri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", file);
intent.setDataAndType(photoUri, type);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION|Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(intent);
}
else{
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri data = Uri.fromFile(file);
intent.setDataAndType(data, type);
context.startActivity(intent);
}
我想在 Android Nougat 的图库中打开保存的图片,但我得到的是带有消息 "Can't load the photo".
的黑色图库页面这是我的代码:
清单
<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>
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>
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) {}
}
}
}
打开图像监听器
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);
}
}
}
也许我为图像生成了错误的路径,但旧版本可以正常工作(我试过 Marshmallow 并且效果很好)。
有人可以帮助我吗?谢谢。
我尝试过的几乎所有应用程序都会遇到同样的问题,我的代码与您的代码相同,而且 google 文档说它看起来是正确的...
我唯一可以为您提供的 "solution" 是针对版本 23(https://source.android.com/source/build-numbers.html) 进行编译以避免文件 URI 的强制严格模式。 当然,如果您正在使用它们,您还必须降级 Android 支持库。
如果有人有更好的想法,请分享...
在 onClick()
的 else
块中,在 Intent
上调用 setData()
设置 Uri
后,调用 addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
Intent
.
目前,其他应用无权使用 Uri
标识的内容。添加 FLAG_GRANT_READ_URI_PERMISSION
就是这样做的。
这在 the FileProvider
documentation 以及有关 Android 应用程序开发的现代书籍中都有介绍。
从移动模拟图库查看图像并不困难,但在牛轧糖中却大不相同,我创建了 运行 次图像,并希望看到该图像多次失败,但最终 运行使用此代码
1.after我写这段代码的长期斗争,不要忘记在 mainfest 文件中添加 fil provider,你可能知道。
MimeTypeMap map = MimeTypeMap.getSingleton();
String ext = MimeTypeMap.getFileExtensionFromUrl(file.getName());
String type = map.getMimeTypeFromExtension(ext);
if (type == null)
type = "*/*";
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// only for gingerbread and newer versions
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri photoUri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", file);
intent.setDataAndType(photoUri, type);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION|Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(intent);
}
else{
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri data = Uri.fromFile(file);
intent.setDataAndType(data, type);
context.startActivity(intent);
}