[英]Open image in gallery with Android 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>
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上試過並且效果很好)。
有人能幫我嗎? 謝謝。
在onClick()
的else
塊中,在Intent
上調用setData()
來設置Uri
,在Intent
上調用addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
。
目前,其他應用程序無權使用Uri
標識的內容。 添加FLAG_GRANT_READ_URI_PERMISSION
做到這一點。
FileProvider
文檔以及有關Android應用程序開發的現代書籍都FileProvider
介紹 。
對於我嘗試過的幾乎所有應用程序,我確實會遇到同樣的問題,我的代碼與您的代碼相同,並且google文檔說它看起來是正確的...
我可以提供的唯一“解決方案”是針對版本23( https://source.android.com/source/build-numbers.html )進行編譯,以避免文件URI的強制嚴格模式。 當然,如果您使用它們,還必須降級Android支持庫。
如果有人有更好的主意,請分享......
從移動模擬畫廊查看圖像並不困難,但在牛軋糖是完全不同的我已經創建了運行時圖像,並希望看到該圖像失敗了很多次但最后運行此代碼
1.經過長時間的努力,我編寫了這段代碼,不要忘記在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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.