尝试使用 Intent 打开文件时出现“找不到媒体"错误 [英] When trying to open a file with Intent getting 'Media not found' error
问题描述
我下载了一个文件并将其保存为临时文件
File outputDir = context.getCacheDir();File f = File.createTempFile(FILE_TYPE_PREFIX, "." + extension,outputDir);
f.exists() 表示文件存在,并且我设置了 f.setReadable(true, false); 以便能够读取它.>
我开始一个新的意图
Intent intent = new Intent(Intent.ACTION_VIEW);intent.setDataAndType(Uri.fromFile(f), mimeType);intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);context.startActivity(意图);
f 返回
/data/user/0/com.app.name/cache/PP_278545395.png
在 Uri.fromFile() 之后
file:///data/user/0/com.app.name/cache/PP_278545395.png
当我尝试打开文件时,它打开了图库,但告诉我找不到媒体".
知道为什么会出现这个问题吗?
getCacheDir()
是 内部存储 用于您的应用.其他应用程序无权访问它.使用 FileProvider
将该内容发布到其他应用.
I download a file and save it as a temporary file using
File outputDir = context.getCacheDir();
File f = File.createTempFile(FILE_TYPE_PREFIX, "." + extension,outputDir);
f.exists() says that the file exists and as well I set f.setReadable(true, false); to be able to read it.
The I start a new Intent
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(f), mimeType);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
the f returns
/data/user/0/com.app.name/cache/PP_278545395.png
and after Uri.fromFile()
file:///data/user/0/com.app.name/cache/PP_278545395.png
As I try to open the file, it opens the Gallery but tells me "Media not found".
Any ideas why this issue exists?
getCacheDir()
is part of internal storage for your app. Other apps do not have access to it. Use FileProvider
to publish that content to other apps.
这篇关于尝试使用 Intent 打开文件时出现“找不到媒体"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!