Search code examples
androidandroid-manifestandroid-intentandroid-input-methodandroid-mediascanner

How to choose a file in Android?


I use the code below to do the operation.

Intent action = new Intent(Intent.ACTION_GET_CONTENT);

    action = action.setType("*/*").addCategory(Intent.CATEGORY_OPENABLE);
    startActivityForResult(action, addEditInstance.INTENT_ATTACH_FILE);

But I get options to choose media files where i get result as

/external/images/media/92

but I am looking for only file names. Is there a way to get the actual file path from this uri?


Solution

  • Hope these code help. It's for image content URI, I guess other media types work as well

    public String getRealPathFromURI(Uri contentUri) {
            String [] proj = {MediaStore.Images.Media.DATA};
            Cursor cursor = managedQuery( contentUri,
                            proj, null, null, null);
            int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
    
            return cursor.getString(columnIndex);
        }