Search code examples
androidimagefileselectandroid-gallery

How to select a file from the gallery?


I want to select a picture/movie file via Android gallery. I followed a similar question in SO: Get/pick an image from Android's built-in Gallery app programmatically and this solves the problem somehow.

The question is, in this code, the returned object is of Bitmap type. How can we return a raw file? I mean, I don't want it to be decoded to Bitmap, I just want to receive the file itself, as is.

The following code is part of the solution to this question: Get/pick an image from Android's built-in Gallery app programmatically

Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};

Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();

int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
final String filePath = cursor.getString(columnIndex);
cursor.close();    

Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
/* Now you have choosen image in Bitmap format in object "yourSelectedImage". You can use it in way you want! */

So, how can I do that?


Solution

  • All you need to do is,

    File myRawFile = new File(filePath);
    

    and then use the file to get what you want. You can read more about File class from here,

    https://developer.android.com/reference/java/io/File.html