当我试图打开一个文件时,应用程序崩溃了。它可以在Android Nougat下运行,但在Android Nougat上它会崩溃。只有当我试图从SD卡,而不是从系统分区打开文件时,它才会崩溃。权限问题?
示例代码:
File file = new File("/storage/emulated/0/test.txt");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "text/*");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent); // Crashes on this line
日志:
android.os.FileUriExposedException:
///storage/emulated/0/test.txt
Intent.getData ()
编辑:
当针对Android Nougat时,file:// uri不再被允许。我们应该使用content:// uri。但是,我的应用程序需要打开根目录下的文件。什么好主意吗?
@palash k答案是正确的,适用于内部存储文件,但在我的情况下,我想从外部存储打开文件,我的应用程序崩溃时从外部存储如sdcard和usb打开文件,但我设法通过修改provider_paths.xml从接受的答案解决了这个问题
像下面那样修改provider_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path path="Android/data/${applicationId}/" name="files_root" />
<root-path
name="root"
path="/" />
</paths>
和在java类(没有改变作为接受的答案,只是一个小编辑)
Uri uri=FileProvider.getUriForFile(getActivity(), BuildConfig.APPLICATION_ID+".provider", File)
这帮助我修复了外部存储文件的崩溃,希望这将帮助一些人有同样的问题,我的
:)
如果你的targetSdkVersion >= 24,那么我们必须使用FileProvider类来访问特定的文件或文件夹,以使其他应用程序可以访问它们。我们创建自己的类继承FileProvider,以确保我们的FileProvider不会与此处所述导入依赖项中声明的FileProviders冲突。
用content:// URI替换file:// URI的步骤:
在AndroidManifest.xml的<application>标签下添加FileProvider <provider>标签。为android:authorities属性指定唯一的权限以避免冲突,导入的依赖项可能指定${applicationId}。提供者和其他常用的授权。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
...
<application
...
<provider
android:name="androidx.core.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>
</application>
</manifest>
然后在res/xml文件夹中创建provider_paths.xml文件。如果一个文件夹还不存在,可能需要创建它。该文件的内容如下所示。它描述了我们希望以external_files的名称共享根文件夹(path=".")的外部存储的访问权限。
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path name="external_files" path="."/>
</paths>
最后一步是更改下面的代码行
Uri photoURI = Uri. fromfile (createImageFile());
来
Uri photoURI = FileProvider。getUriForFile(context, context. getapplicationcontext (). getpackagename() +”。提供者”,createImageFile ());
编辑:如果你想让系统打开你的文件,你可能需要添加以下代码行:
intent.addFlags (Intent.FLAG_GRANT_READ_URI_PERMISSION);
请参考这里解释的完整代码和解决方案。
以下是我的解决方案:
在Manifest.xml
<application
android:name=".main.MainApp"
android:allowBackup="true"
android:icon="@drawable/ic_app"
android:label="@string/application_name"
android:logo="@drawable/ic_app_logo"
android:theme="@style/MainAppBaseTheme">
<provider
android:name="androidx.core.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>
在res / xml / 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>
在我的片段中,我有下面的代码:
Uri myPhotoFileUri = FileProvider.getUriForFile(getActivity(), getActivity().getApplicationContext().getPackageName() + ".provider", myPhotoFile);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.putExtra(MediaStore.EXTRA_OUTPUT, myPhotoFileUri);
Тhat就是你所需要的。
也不需要创造
public class GenericFileProvider extends FileProvider {}
我在Android 5.0, 6.0和Android 9.0上进行了测试,并取得了成功。
我不知道为什么,我所做的一切与Pkosta (https://stackoverflow.com/a/38858040)完全相同,但一直得到错误:
Permission Denial:从ProcessRecord{reacted} (reacted)打开提供者,而不是从uid reacted导出
我在这个问题上浪费了好几个小时。罪魁祸首吗?芬兰湾的科特林。
val playIntent = Intent(Intent.ACTION_VIEW, uri)
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
intent实际上是设置getIntent()。addFlags而不是在我新声明的playIntent上操作。