In Android, How to obtain Uri of image on SD-Card
When building an Android app, you often need to fetch an image stored on the device’s external storage (like an SD card) and do something with it—whether that’s displaying it in a view or passing it to another app.
Getting the URI for a file isn’t complicated at all, but it’s a super handy snippet to keep in your back pocket. Here is how you can do it in a couple of quick steps.
Step 1: Locate the Image File
First, we need to grab a reference to the image on the SD card. We can find the root external storage directory using Environment and then map out the specific path to our target file:
File sdCardDir = Environment.getExternalStorageDirectory();
File yourFile = new File(sdCardDir, "path/to/the/file/filename.ext");Step 2: Generate the URI
Once you have your File object ready to go, converting it into a usable URI is incredibly easy:
Uri imageUri = yourFile.toUri();3. Put the URI to Work
Now your URI is ready for action. For example, if you want to trigger an implicit intent that opens up the system gallery or an image viewer to display that exact photo, you can pass the URI straight in like this:
Intent intent = new Intent(Intent.ACTION_VIEW, imageUri);
startActivity(intent);And that’s pretty much all there is to it! Nor very tough, but love sharing this.