android - Android - 在 android 库中显示图像
问题描述
我需要打开一张照片,我有红色可以使用默认的 android 画廊,但我无法让它工作。
我查看了几个论坛,最后我得到了这个代码,但它打开了一个黑色的图像。
我正在使用 api 最低 16 和目标 29,在 api 29 上进行测试。
有人可以帮助我吗?
谢谢。
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("content://"+"/storage/emulated/0/photo.jpg"), "image/*");
startActivity(intent);
已经尝试过 Uri.fromFile() 但不起作用。
解决方案
您可以改用 Uri.fromFile()
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File("/storage/emulated/0/photo.jpg")), "image/*");
startActivity(intent);
但是您会收到 FileURIExposed 错误,因此您需要在 oncreate() 中添加此代码
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build);
注意:不要使用硬编码路径使用环境
不是最好的答案,但效果很好
推荐阅读
- python - 如何将 python time.time() 转换为日期格式以进行 sql 插入
- asp.net-core - 从 Entity Framework Core 中的 cookie 和基本路径创建数据库上下文
- javascript - 从资产文件夹打开本地 html 时,window.open() 弹出窗口被广告拦截器阻止,如果文件没有 .html 扩展名,则工作正常
- python - 如何将列表输入到函数中并在 Python 中接收列表?
- javascript - 指示 id 以确认 recatjs 中的操作时弹出错误
- c# - 显式等待不同的形式
- ruby-on-rails - Rails 指定所需的范围
- python - 我需要从python中的列表中提取每个字典吗?
- java - 如何在 Java 中打开 Excel 工作簿并删除所有只有标题行的工作表
- jquery - :active 伪类无法正常工作