[英]How to open files (from SD-Card) in gallery
我保存了不同jpg的路徑。 數據庫中的文件。 這些圖像部分在內部存儲器中,部分在外部存儲器(SD卡)中。
現在,我想打開圖庫中的圖片。 要使用內部存儲器打開圖像,請使用FileProvider,方法如下:
String path = "/storage/emulated/0/Pictures/Meetings/IMG_Testmeeting20180809_225414.jpg";
File image = new File(path);
Uri uri = FileProvider.getUriForFile(getContext(), "com.meetings.martin.provider",image);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "image/*");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
getContext().startActivity(intent);
這是我在AndroidManifest.xml中的FileProvider:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.meetings.martin.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths">
</meta-data>
</provider>
這是file_paths.xml的內容:
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images" path="." />
</paths>
這可以正常工作....但是,如果我要打開SD卡上的圖像,則會收到以下錯誤消息:
Failed to find configured root that contains <path>
我經常讀到FileProvider無法訪問SD卡上的文件。 但是,如何將路徑變成Uri並在圖庫中打開呢?
我解決了 我在路徑文件中添加了以下行:
<root-path name="external_files" path="/storage/" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.