Android 中的 FileUriExposedException [英] FileUriExposedException in Android

查看:19
本文介绍了Android 中的 FileUriExposedException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要打开内部存储中包含图像的文件夹.

I need to open folder in internal storage that contains images.

我使用以下代码.

Java

 File folder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),  "MyPhotos");
Intent intent = new Intent(Intent.ACTION_VIEW);
String path =folder.getPath();
Uri myImagesdir = Uri.parse("file://" + path );
intent.setDataAndType(myImagesdir,"*/*");   
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);

路径

 <paths xmlns:android="http://schemas.android.com/apk/res/android">
     <external-path name="images" path="Pictures" />
     <external-path name="external_files" path="."/>
     <external-path name="files_root" path="Android/data/${applicationId}"/> </paths>

清单

  <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.example.android.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>

xml/file_paths

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="images" path="Pictures" />
    <external-path name="external_files" path="."/>
    <external-path name="files_root" path="Android/data/${applicationId}"/>
</paths>

错误

致命异常:主进程:android.apps.bnb.company.myphotos,PID:22482android.os.FileUriExposedException: file:///storage/emulated/0/Pictures/MyPhotos 暴露在应用程序之外通过 Intent.getData()

FATAL EXCEPTION: main Process: android.apps.bnb.company.myphotos, PID: 22482 android.os.FileUriExposedException: file:///storage/emulated/0/Pictures/MyPhotos exposed beyond app through Intent.getData()

还有其他方法可以打开内部存储中的文件夹吗?谢谢!

Is any another way to open folder in internal storage? Thanks!

使用这个 arcticle https://inthecheesefactory.com/blog/how-to-share-access-to-file-with-fileprovider-on-android-nougat/en 我替换了

Using this arcticle https://inthecheesefactory.com/blog/how-to-share-access-to-file-with-fileprovider-on-android-nougat/en I replaced

Uri myImagesdir = Uri.parse("file://" + path );

 Uri myImagesdir = Uri.parse("content://" + path );

错误消失了.

无论如何我必须选择始终应用程序来打开此文件夹.

Anyway I have to choose always app to open this folder.

是否可以默认使用我的文件"应用打开某个文件夹?

推荐答案

我在这里找到了解决方案我们可以通过以下方式打开下载文件夹吗?意图?

I found solution here can we open download folder via. intent?

此代码在我的三星 J7 中完美运行,可以使用三星默认应用程序我的文件从内存中打开图片文件夹(和其他文件夹).

This code works perfect in my Samsung J7 to open Pictures folder (and others) from internal memory using Samsung default application My files.

File path = new File(Environment.getExternalStorageDirectory(), Environment.DIRECTORY_PICTURES);
Uri uri = Uri.fromFile(path);
Intent intent = getPackageManager().getLaunchIntentForPackage("com.sec.android.app.myfiles");
intent.setAction("samsung.myfiles.intent.action.LAUNCH_MY_FILES");
                    intent.putExtra("samsung.myfiles.intent.extra.START_PATH", path.getAbsolutePath());
startActivity(intent);

看来我们必须反编译每个制造商的文件管理器,看看如何正确调用它.:( 而且工作量太大了.好吧......我认为有一些通用的解决方案可以做到这一点.

It seems like we have to decompile File Manager of each manufacturer to see how to call it properly. :( And it is too much work. Well... I assumed there is some generic solution to do it.

这篇关于Android 中的 FileUriExposedException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆