当前位置: 首页 > 面试题库 >

Android中的FileUriExposedException

朱令
2023-03-14
问题内容

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

我使用以下代码。

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:22482
android.os.FileUriExposedException:file:/// storage / emulated / 0 /
Pictures / MyPhotos通过Intent.getData()暴露给应用程序

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

更新#1

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

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

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

错误消失了。

无论如何,我必须选择总是应用程序来打开此文件夹。

默认情况下是否可以使用“我的文件”应用打开某些文件夹?


问题答案:

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

该代码在我的Samsung J7中非常有效,可以使用Samsung默认应用程序 My files
从内部存储器打开Pictures文件夹(和其他文件夹)。

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);

似乎我们必须对每个制造商的 文件管理器 进行反编译,才能了解如何正确调用它。:(而且工作量太大。嗯…我想有一些通用的解决方案可以做到这一点。



 类似资料:
  • 我的年级代码是: 请帮我解决这个问题。

  • 我需要打开包含图像的内部存储文件夹。 清单 xml/file_paths 错误 无论如何,我必须选择始终应用程序打开这个文件夹。 是否可以默认使用我的文件应用程序打开某个文件夹?

  • 我已经为android Studio创建了一个webview应用程序。但没有加载web URL。错误为NET::ERR_ACCESS_DENIED。有谁能帮忙吗

  • 问题内容: 我正在尝试创建一个可以验证人脸的Android应用程序,但是当我尝试在模拟器上运行应用程序(使用Eclipse)时,在logcat中得到以下结果: FaceVerificationApplication.java的代码如下: 我该如何解决此异常?任何建议都会有很大帮助。 问题答案: 嗨,我的问题解决了。我必须将jna-4.2.2.jar添加到我的项目中。我在jna.jar中遇到有关本机

  • 问题内容: 我有很多文件的Java项目,正在使用LOG4J。现在,我正在尝试将其移植到Android平台。是否可以通过LOG4J函数调用按原样重用代码? 目前的理解: 属性配置不起作用(依赖于bean) 我尝试使用LOG4J for Android和SL4J Lib。没有成功 加工。但是没用 我想念什么吗?指向任何可行的例子? 问题答案: 通过使用android-logging- log4j.ja

  • 我有一个问题,找出我的源代码中的错误。 有什么办法可以解决它吗? 我的代码中抛出了“ArrayIndexOutOfBoundsException”,但我无法找到它的位置。 android studio中的logcat: