无法使用自定义文件提供程序在Nougat中打开文件

时间:2017-08-09 06:43:46

标签: android android-7.0-nougat internal-storage

我正在尝试使用文件提供程序在nougat中打开一个文件,查看一些示例并添加提供程序以显示并创建providerpath xml,如下所示:

<provider
    android:name="android.support.v4.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>

provider_path.xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path
        name="/"
        path="." />
</paths>

打开文件的代码:

            Intent intent = new Intent();
            intent.setAction(android.content.Intent.ACTION_VIEW);
            File file = new File(path);

            MimeTypeMap mime = MimeTypeMap.getSingleton();
            String ext = file.getName().substring(file.getName().indexOf(".") + 1);
            String type = mime.getMimeTypeFromExtension(ext);
            Log.d(TAG, file.getAbsolutePath() + "mime " + type + " ext " + ext + "uri file " + FileProvider.getUriForFile(context,
                    BuildConfig.APPLICATION_ID + ".provider",
                    file).toString());
            Uri ur = UriHelper.getNougatUri(file);
            Log.d(TAG, "uri2 " + ur);
            intent.setDataAndType(FileProvider.getUriForFile(context,
                    BuildConfig.APPLICATION_ID + ".provider",
                    file), type);
            context.startActivity(intent);

logcat的:

08-09 12:04:39.728 18288-18288/com.app.tod D/ChatAdapter1: /storage/emulated/0/com.app.tod/Media/sent/08-09-2017 12:04 PM.jpegmime image/jpeg ext jpeguri file content://com.app.tod.provider/%2Fstorage%2Femulated%2F0%2Fcom.app.tod%2F/com.app.toado/Media/sent/08-09-2017%2012%3A04%20PM.jpeg
08-09 12:04:39.729 18288-18288/com.app.tod D/ChatAdapter1: uri2 content://com.app.tod.provider/storage/emulated/0/com.app.tod/Media/sent/08-09-2017 12:04 PM.jpeg

当我点击文件时,它会进入图库并说无法加载图片, 图像存在于此路径 -

/storage/emulated/0/com.app.tod/Media/sent/08-09-2017 12:04 PM.jpeg

有人可以告诉我如何在/storage/emulated/0/com.app.tod /

访问我为我的应用创建的文件夹中的所有文件

另外,如果我想在内部存储中的某个其他位置打开文件,还有办法吗?

感谢。

1 个答案:

答案 0 :(得分:1)

  

当我点击文件时,它会进入图库并说无法加载图像

您没有将FLAG_GRANT_READ_URI_PERMISSION添加到Intent,因此第三方应用无法读取Uri标识的内容。

另请注意,您的UriHelper.getNougatUri(file);似乎没用,因为您对结果所做的只是将其记录到LogCat。

相关问题