Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android get Gallery image Uri path

In an Activity, I can choose an image from the Gallery, and I need its Uri path (in the log, the Uri path for my test image is /content:/media/external/images/media/1).

I'm getting this error though:

08-04 02:14:21.912: DEBUG/PHOTOUPLOADER(576): java.io.FileNotFoundException: /content:/media/external/images/media/1 (No such file or directory) 08-04 02:14:32.124: WARN/System.err(576): java.io.FileNotFoundException: /content:/media/external/images/media/1 (No such file or directory) 

Is this the correct format of a file path? Or should I make it to be something like sdcard\...\image.png?

like image 535
Allan Jiang Avatar asked Aug 04 '11 02:08

Allan Jiang


People also ask

What is the gallery path in Android?

"gallery" is an app, not a location. Your pictures on your phone could be located anywhere, depending on how they got on your phone. Your camera will store its images at "/DCIM/camera", or a similar location. Social media apps may download photos into the "/download" folder or a folder under the app's name.

What is Uri of an image?

URI or Uniform Resource Identifier is a compact sequence of characters that identifies an abstract or physical resource. It can be further classified as a locator, a name, or both. Basically URI (in some cases URL or URN) will point to the image location, or will be the name of the image (why not both?).

How do you find the absolute path of URI?

Uri uri = data. getData(); File file = new File(uri. getPath());//create path from uri final String[] split = file. getPath().


2 Answers

public String getPath(Uri uri) {     String[] projection = { MediaStore.Images.Media.DATA };     Cursor cursor = managedQuery(uri, projection, null, null, null);     startManagingCursor(cursor);     int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);     cursor.moveToFirst();     return cursor.getString(column_index); } 
like image 120
Allan Jiang Avatar answered Oct 20 '22 09:10

Allan Jiang


val inputStream = context.contentResolver.openInputStream(uri) 

is all you need.

like image 42
Saket Avatar answered Oct 20 '22 11:10

Saket