android - 在 OS 6 及更高版本中无法打开 SD 卡文件
问题描述
我有一种方法可以从我的应用程序中打开文件,并且此方法在每个操作系统上运行良好以进行内部存储,但是当来自 OS 6 及更高版本的 sdcard 想要打开文件时,我发现了一个错误:
找不到包含 /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 manifest 上。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
推荐阅读
- session - Google Analytics 用户指标是否等同于 Webtrends Analytics 访问者指标?
- sql - 连接表中最小值的显示名称
- ios - TestFlight 后无法从 Firebase 获得推送通知
- reactjs - Deck.GL ScatterplotLayer 不渲染点
- python - Python - 从请求中打印特定的 cookie 值
- sql-server - SQL Server - 插入事务期间性能不佳
- javascript - 未找到 sinon 存根类
- python - 当我尝试运行 get-pip.py 时,它告诉我“已成功卸载 Pip”
- android - Android 上 Facebook 登录 Firebase 的问题 - E/GraphResponse
- typo3 - 列表查看新记录类别标签名称 TYPO3