在Android的默认图库图像查看器中使用URI打开图像

我已经提取了图像uri,现在我想用Android的默认图像查看器打开图像。 或者甚至更好,用户可以select使用什么程序来打开图像。 像文件探索者一样,如果你尝试打开一个文件提供给你。

接受的答案不适合我,

什么工作:

Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setDataAndType(Uri.parse("file://" + "/sdcard/test.jpg"), "image/*"); startActivity(intent); 

问自己,也回答自己:

 startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("content://media/externalhttp://img.dovov.commedia/16"))); /** replace with your own uri */ 

它还会询问用什么程序来查看文件。

尝试使用它:

 Uri uri = Uri.fromFile(entry); Intent intent = new Intent(android.content.Intent.ACTION_VIEW); String mime = "*/*"; MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton(); if (mimeTypeMap.hasExtension( mimeTypeMap.getFileExtensionFromUrl(uri.toString()))) mime = mimeTypeMap.getMimeTypeFromExtension( mimeTypeMap.getFileExtensionFromUrl(uri.toString())); intent.setDataAndType(uri,mime); startActivity(intent); 

基于Vikas的答案,但略有修改:Uri收到的参数:

 private void showPhoto(Uri photoUri){ Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setDataAndType(photoUri, "image/*"); startActivity(intent); } 

如果您的应用程序的目标是Android N(7.0)及以上版本,则不应使用上面的(“Uri.fromFile”方法)的答案,因为它不适用于您。

相反,你应该使用一个ContentProvider。

例如,如果你的图像文件在外部文件夹中,你可以使用这个(类似于我在这里所做的代码):

 File file = ...; final Intent intent = new Intent(Intent.ACTION_VIEW)// .setDataAndType(VERSION.SDK_INT >= VERSION_CODES.N ? android.support.v4.content.FileProvider.getUriForFile(this,getPackageName() + ".provider", file) : Uri.fromFile(file), "image/*").addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 

performance:

 <provider android:name="android.support.v4.content.FileProvider" android:authorities="${applicationId}.provider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths"/> </provider> 

res / xml / provider_paths.xml:

 <?xml version="1.0" encoding="utf-8"?> <paths> <!--<external-path name="external_files" path="."/>--> <external-path name="files_root" path="Android/data/${applicationId}"/> <external-path name="external_storage_root" path="."/> </paths> 

如果您的图片位于应用程序的私人path中,您应该创build自己的ContentProvider,因为我在链接上创build了“OpenFileProvider”。

一个更清洁,更安全的回答这个问题(你真的不应该硬编码string):

 public void openInGallery(String imageId) { Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI.buildUpon().appendPath(imageId).build(); Intent intent = new Intent(Intent.ACTION_VIEW, uri); startActivity(intent); } 

您所要做的就是将图像ID附加到EXTERNAL_CONTENT_URIpath的末尾。 然后用View动作和Uri启动一个Intent。

图片ID来自查询内容parsing器。

所有上述答案不开放形象..当我第二次尝试打开它显示画廊不是图像。

我从各种不同的答案混合解决scheme..

 Intent galleryIntent = new Intent(Intent.ACTION_VIEW, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); galleryIntent.setDataAndType(Uri.fromFile(mImsgeFileName), "image/*"); galleryIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(galleryIntent); 

这个只对我有用

我用这个对我有用

 Intent intent = new Intent(); intent.setType("image/*"); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(Intent.createChooser(intent, "Select Picture"), 1); 

这个东西可能会有帮助,如果你使用Android N及以下

  File file=new File(Environment.getExternalStorageDirectory()+"/directoryname/"+filename); Uri path= FileProvider.getUriForFile(MainActivity.this,BuildConfig.APPLICATION_ID + ".provider",file); Intent intent=new Intent(Intent.ACTION_VIEW); intent.setDataAndType(path,"image/*"); intent.setFlags(FLAG_GRANT_READ_URI_PERMISSION | FLAG_GRANT_WRITE_URI_PERMISSION); //must for reading data from directory 

几乎没有机会使用照片或图库应用程序(可能存在一个),但您可以尝试内容查看器。

请检查这里类似的问题的另一个答案

显示使用Intent.ACTION_VIEW文件的问题是,如果您通过Uriparsingpath。 在所有情况下都不起作用。 要解决这个问题,你需要使用:

 Uri.fromFile(new File(filePath)); 

代替:

 Uri.parse(filePath); 

编辑

这是我的完整代码:

 Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(new File(mediaFile.filePath)), mediaFile.getExtension()); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); 

信息

MediaFile是我的域类,用于将数据库中的文件封装在对象中。 MediaFile.getExtension()返回一个带MimetypeString作为文件扩展名。 例如: "image/png"


附加代码:需要显示任何文件(扩展名)

 import android.webkit.MimeTypeMap; public String getExtension () { MimeTypeMap myMime = MimeTypeMap.getSingleton(); return myMime.getMimeTypeFromExtension(MediaFile.fileExtension(filePath)); } public static String fileExtension(String path) { if (path.indexOf("?") > -1) { path = path.substring(0, path.indexOf("?")); } if (path.lastIndexOf(".") == -1) { return null; } else { String ext = path.substring(path.lastIndexOf(".") + 1); if (ext.indexOf("%") > -1) { ext = ext.substring(0, ext.indexOf("%")); } if (ext.indexOf("/") > -1) { ext = ext.substring(0, ext.indexOf("/")); } return ext.toLowerCase(); } } 

让我知道如果你需要更多的代码。