android - Sdcard 文件无法在 OS 6 及更高版本中打开

标签 android android-external-storage fileprovider

我有一种方法可以从我的应用程序中打开文件,这种方法在内部存储的每个操作系统上运行良好,但是当 OS 6 和更高版本的 sdcard 想要打开文件时,我发现了一个错误:

Failed to find the configured root that contains /storage/BE02-07BA/WhatsApp/Media/WallPaper/download (1).jpg

我的代码如下:

try {
    File f = new File(feedItem.getFilePath());
    MimeTypeMap map = MimeTypeMap.getSingleton();
    String url = f.getName();
    url = URLEncoder.encode(url, "UTF-16").replace("+", "%20");
    String ext = MimeTypeMap.getFileExtensionFromUrl(url);
    String type = map.getMimeTypeFromExtension(ext.toLowerCase());
    if (type == null)
        type = "*/*";
    Uri uri = Uri.parse("www.google.com");
    Intent type_intent = new Intent(Intent.ACTION_VIEW, uri);
    Uri data = FileProvider.getUriForFile(mContext, BuildConfig.APPLICATION_ID + ".provider",f);
    type_intent.setDataAndType(data, type);
    type_intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

    mContext.startActivity(type_intent);
} catch (Exception e) {
    // Auto-generated catch block
    e.printStackTrace();
}

最佳答案

试着把它放在你的 android list 上。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

关于android - Sdcard 文件无法在 OS 6 及更高版本中打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53863913/

相关文章:

Android - 如何使用新的存储访问框架将文件复制到外部 SD 卡

android - 调用 ACTION_GET_CONTENT Intent 后文件不可选择

android - 警告 : [XmlResourcesTransformer] No mapping for: android/support/FILE_PROVIDER_PATHS

php - 来自 android 的实时 javascript 请求或从 Controller Cakephp/php 调用 javascript 函数

java - 将汉字重新分配给自定义字符代码

Android Studio 无法通过 SVN 进行身份验证

android - 如何在 Android 10 中访问外部存储?

android - 使用 FileProvider Android 从可移动 SD 卡打开文件时出错

c# - GetDirectoryContents 方法不显示超过 28MB 的文件

java - Android:java.util.logging.Logger 与 android.util.Log