SoFunction
Updated on 2025-04-09

Android open mobile album to get picture path

This article shares the specific code for Android opening the mobile album to get the picture path for your reference. The specific content is as follows

Get the real SD card path to the photo based on Uri uri = (); when you turn on the camera!

Permissions

<uses-permission android:name=".READ_EXTERNAL_STORAGE" />
<uses-permission android:name=".WRITE_EXTERNAL_STORAGE" />

Dynamic application permission

String[] mPermissionList = new String[]{
   .WRITE_EXTERNAL_STORAGE,
   .READ_EXTERNAL_STORAGE};
 public static final int REQUEST_PICK_IMAGE = 11101;

Open the album of your phone

(, mPermissionList, 100);
@Override
 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  (requestCode, permissions, grantResults);
  switch (requestCode) {
   case 100:
    boolean writeExternalStorage = grantResults[0] == PackageManager.PERMISSION_GRANTED;
    boolean readExternalStorage = grantResults[1] == PackageManager.PERMISSION_GRANTED;
    if ( &gt; 0 &amp;&amp; writeExternalStorage &amp;&amp; readExternalStorage) {
     getImage();
    } else {
     (this, "Please set the necessary permissions", Toast.LENGTH_SHORT).show();
    }

    break;
  }
 }

 private void getImage() {
  if (.SDK_INT &lt; Build.VERSION_CODES.KITKAT) {
   startActivityForResult(new Intent(Intent.ACTION_GET_CONTENT).setType("image/*"),
     REQUEST_PICK_IMAGE);
  } else {
   Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
   (Intent.CATEGORY_OPENABLE);
   ("image/*");
   startActivityForResult(intent, REQUEST_PICK_IMAGE);
  }
 }

Get the Uri returned by the album

@Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  (requestCode, resultCode, data);
  if (resultCode == Activity.RESULT_OK) {
   switch (requestCode) {
    case REQUEST_PICK_IMAGE:
     if (data != null) {
      String realPathFromUri = (this, ());
     } else {
      (this, "The picture is damaged, please select again", Toast.LENGTH_SHORT).show();
     }

     break;
   }
  }
 }

The return of Uri here is, realPathFromUri is the real path

Get the real path

package ;

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;

public class RealPathFromUriUtils {
 /**
   *Absolute path to get the image according to Uri
   *
   * @param context context object
   * @param uri Uri Pictures
   * @return If the image corresponding to Uri exists, then return the absolute path of the image, otherwise return null
   */
 public static String getRealPathFromUri(Context context, Uri uri) {
  int sdkVersion = .SDK_INT;
  if (sdkVersion &gt;= 19) { // api &gt;= 19
   return getRealPathFromUriAboveApi19(context, uri);
  } else { // api &lt; 19
   return getRealPathFromUriBelowAPI19(context, uri);
  }
 }

 /**
   * Apply to API19 below (excluding API19), obtain the absolute path of the image according to URI
   *
   * @param context context object
   * @param uri Uri Pictures
   * @return If the image corresponding to Uri exists, then return the absolute path of the image, otherwise return null
   */
 private static String getRealPathFromUriBelowAPI19(Context context, Uri uri) {
  return getDataColumn(context, uri, null, null);
 }

 /**
   * Adapt to API19 and above, get the absolute path of the image based on uri
   *
   * @param context context object
   * @param uri Uri Pictures
   * @return If the image corresponding to Uri exists, then return the absolute path of the image, otherwise return null
   */
 @SuppressLint("NewApi")
 private static String getRealPathFromUriAboveApi19(Context context, Uri uri) {
  String filePath = null;
  if ((context, uri)) {
   // If it is a document type uri, it will be processed through the document id   String documentId = (uri);
   if (isMediaDocument(uri)) { // MediaProvider
    // Use ':' to split    String id = (":")[1];

    String selection = ._ID + "=?";
    String[] selectionArgs = {id};
    filePath = getDataColumn(context, .EXTERNAL_CONTENT_URI, selection, selectionArgs);
   } else if (isDownloadsDocument(uri)) { // DownloadsProvider
    Uri contentUri = (("content://downloads/public_downloads"), (documentId));
    filePath = getDataColumn(context, contentUri, null, null);
   }
  } else if ("content".equalsIgnoreCase(())) {
   // If it is content type Uri   filePath = getDataColumn(context, uri, null, null);
  } else if ("file".equals(())) {
   // If it is a file type Uri, directly get the path corresponding to the image   filePath = ();
  }
  return filePath;
 }

 /**
   * Get the _data column in the database table, that is, return the file path corresponding to Uri
   *
   * @return
   */
 private static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
  String path = null;

  String[] projection = new String[]{};
  Cursor cursor = null;
  try {
   cursor = ().query(uri, projection, selection, selectionArgs, null);
   if (cursor != null &amp;&amp; ()) {
    int columnIndex = (projection[0]);
    path = (columnIndex);
   }
  } catch (Exception e) {
   if (cursor != null) {
    ();
   }
  }
  return path;
 }

 /**
  * @param uri the Uri to check
  * @return Whether the Uri authority is MediaProvider
  */
 private static boolean isMediaDocument(Uri uri) {
  return "".equals(());
 }

 /**
  * @param uri the Uri to check
  * @return Whether the Uri authority is DownloadsProvider
  */
 private static boolean isDownloadsDocument(Uri uri) {
  return "".equals(());
 }
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.