android外部文件,android – 如何在外部缓存目录中为文件设置FileProvider
对于Cache目录中的文件,我可以在xml中添加它来设置FileProvidername="image"path="image/"/>但是,如果我在外部缓存目录中存储文件,我无法获得外部缓存路径标记或类似的东西来设置FileProvider.Andname="image_external"path="cache/image/"/>也没有帮助.这是我的清单文件:android:name=
对于Cache目录中的文件,我可以在xml中添加它来设置FileProvider
name="image"
path="image/"/>
但是,如果我在外部缓存目录中存储文件,我无法获得外部缓存路径标记或类似的东西来设置FileProvider.And
name="image_external"
path="cache/image/"/>
也没有帮助.
这是我的清单文件:
android:name="android.support.v4.content.FileProvider"
android:authorities="com.tizi.quanzi"
android:exported="false"
android:grantUriPermissions="true">
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/>
这是xml / file_paths文件:
name="image"
path="image/"/>
name="image_external"
path="cache/image/"/>
这是我的代码:
String RootPath = App.getApplication().getExternalCacheDir().getAbsolutePath();
String filePath = RootPath + "/image/" + fileName;
// done something there to save file
Intent shareIntent = new Intent();
Uri contentUri = FileProvider.getUriForFile(App.getApplication(),
App.getApplication().getPackageName(), new File(filePath));
App.getApplication().grantUriPermission(App.getApplication().getPackageName(),
contentUri, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
shareIntent.setData(contentUri);
shareIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, contentUri);
shareIntent.setType("image/*");
activity.startActivity(Intent.createChooser(shareIntent, "share image"));
这是错误信息:
/AndroidRuntime: FATAL EXCEPTION: main
Process: com.tizi.quanzi, PID: 27487
java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Android/data/com.tizi.quanzi/cache/image/o9xygODHtdP6HXqsuUZghVCsBKTtY4FJgO1MpnmX.jpg
at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:678)
at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:377)
at com.tizi.quanzi.tool.ShareImage.shareImage(ShareImage.java:67)
at com.tizi.quanzi.tool.ShareImage.shareImage(ShareImage.java:61)
at com.tizi.quanzi.adapter.GalleryAdapter$2.onClick(GalleryAdapter.java:119)
at android.support.v7.app.AlertController$AlertParams$3.onItemClick(AlertController.java:956)
at android.widget.AdapterView.performItemClick(AdapterView.java:310)
at android.widget.AbsListView.performItemClick(AbsListView.java:1145)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3042)
at android.widget.AbsListView$3.run(AbsListView.java:3879)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
更多推荐
所有评论(0)