How to get a file's name and uri address from the Media Store

Asked 2 years ago, Updated 2 years ago, 125 views

I made an onActivityResult to get a URI for the file I selected in the media store Uri selectedImage = data.getData(); If you change this to String,

content://media/external/images/media/47 or /external/images/media/47 when used as the path But is there any way to know this as an absolute path?Because I want to copy the image to the place I followed, and if it's numbered in the URI, the numbering will change when the device is turned off and on...

android uri mediastore absolute-path

2022-09-21 14:19

1 Answers

Absolute path is

public String getRealPathFromURI(Context context, Uri contentUri) {
  Cursor cursor = null;
  try { 
    String[] proj = { MediaStore.Images.Media.DATA };
    cursor = context.getContentResolver().query(contentUri,  proj, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
  } } finally {
    if (cursor != null) {
      cursor.close();
    }
  }
}

If you do this, you can change it.


2022-09-21 14:19

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.