我的应用中有几张图片,我想在用户点击它们时在默认查看器中打开它们。这是我的代码:
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse("file://" + Environment.getExternalStorageDirectory().toString() + "/Pictures/mypic.jpg");
intent.setData(uri);
intent.setType("image/jpeg");
startActivity(intent);
默认查看器确实已打开,但它会显示其主屏幕",显示所有文件夹和图库。它不会显示所选图片。
我做错了什么?
答案 0 :(得分:1)
将以下代码放在try块中,您可以打开任何文件: 添加捕获阻止你自己。
String FileName = ...full path of file...
MimeTypeMap map = MimeTypeMap.getSingleton();
String extension = map.getFileExtensionFromUrl(FileName.toLowerCase().replace(" ", "")); // does not work with spaces in filename
String mimetype = map.getMimeTypeFromExtension(extension);
Toast.makeText(context, FileName + "\nMimeType: " + mimetype + "\nExtension: " + extension , Toast.LENGTH_SHORT).show();
if ( mimetype == null )
{
mimetype = "text/plain";
Toast.makeText(context, "MimeType: " + mimetype, Toast.LENGTH_SHORT).show();
}
String Url = "file://" + FileName;
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(Url), mimetype);
context.startActivity(intent);