Open an image using a URI in the default image gallery viewer application in Android

I extracted the uri image, now I would like to open the image using the default Android image viewer. Or even better, the user can choose which program to use to open the image. Something like File Explorers offers you if you try to open a file.

+74
android android-image
Mar 21 2018-11-21T00:
source share
13 answers

Ask yourself, answer yourself:

startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("content://media/external/images/media/16"))); /** replace with your own uri */ 

He will also ask which program to use to view the file.

+27
Mar 21 2018-11-21T00:
source share

The accepted answer did not work for me,

What worked:

 Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setDataAndType(Uri.parse("file://" + "/sdcard/test.jpg"), "image/*"); startActivity(intent); 
+155
Jul 21 2018-11-11T00:
source share

If your application is designed for Android N (7.0) and higher, you should not use the answers above (the "Uri.fromFile" method), because it will not work for you.

Instead, you should use ContentProvider.

For example, if your image file is in an external folder, you can use this (similar to the code I made here ):

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

manifest:

 <provider android:name="androidx.core.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> 

If your image is in the personal path of the application, you must create your own ContentProvider, as I created "OpenFileProvider" by reference.

+22
Dec 31 '16 at 20:17
source share

Try using it:

 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); 
+19
Sep 29 '12 at 5:30
source share

Based on Vikas Answers , but with a slight modification: Uri obtained by parameter:

 private void showPhoto(Uri photoUri){ Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setDataAndType(photoUri, "image/*"); startActivity(intent); } 
+17
May 18 '15 at 14:54
source share

This thing can help if you work with android N and below.

  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 
+8
Feb 09 '17 at 19:08
source share

A much cleaner and safer answer to this problem (you really shouldn't hardcode the strings)

 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); } 

All you have to do is add the image id at the end of the path for EXTERNAL_CONTENT_URI . Then run Intent with the View and Uri action.

The image identifier comes from the request of the content recognizer.

+5
Mar 14 '13 at 22:59
source share

All the answers above do not open the image .. when the second time I try to open it, show the gallery not the image.

I got a solution from various SO answers.

 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); 

This one just worked for me.

+4
Oct 31 '16 at 6:45
source share

The problem with displaying the file with Intent.ACTION_VIEW is that if you pass Uri path parsing. It does not work in all cases. To fix this problem, you need to use:

 Uri.fromFile(new File(filePath)); 

Instead:

 Uri.parse(filePath); 



Edit

Here is my complete code:

 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); 



Info

MediaFile is my domain class for transferring files from a database to objects. MediaFile.getExtension() returns a String with a Mimetype for the file extension. Example: "image/png"




Additional code: required to display any file (extension)

 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(); } } 

Let me know if you need more code.

+4
Nov 17 '17 at 2:13
source share

I use it, it works for me

 Intent intent = new Intent(); intent.setType("image/*"); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(Intent.createChooser(intent, "Select Picture"), 1); 
+3
Mar 19 '14 at 15:00
source share

There is almost NO possibility to use the application for a photo or gallery (one may exist), but you can try the content viewer.

Please check another answer to a similar question here.

0
Jul 13 '15 at 11:35
source share

My decision

 Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory().getPath()+"/your_app_folder/"+"your_picture_saved_name"+".png")), "image/*"); context.startActivity(intent); 
0
Apr 27 '18 at 13:13
source share

My solution using a file provider

  private void viewGallery(File file) { Uri mImageCaptureUri = FileProvider.getUriForFile( mContext, mContext.getApplicationContext() .getPackageName() + ".provider", file); Intent view = new Intent(); view.setAction(Intent.ACTION_VIEW); view.setData(mImageCaptureUri); List < ResolveInfo > resInfoList = mContext.getPackageManager() .queryIntentActivities(view, PackageManager.MATCH_DEFAULT_ONLY); for (ResolveInfo resolveInfo: resInfoList) { String packageName = resolveInfo.activityInfo.packageName; mContext.grantUriPermission(packageName, mImageCaptureUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION); } view.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION); Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setDataAndType(mImageCaptureUri, "image/*"); mContext.startActivity(intent); } 
0
Jul 17 '19 at 9:48
source share



All Articles