get Real Image Path From URI - Android android.graphics

Android examples for android.graphics:Image Load Save

Description

get Real Image Path From URI

Demo Code

import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore.Images;

public class Main {

  /**//from w  w  w.  jav a2  s  .  com
   * Get path of image from uri
   *
   * @param contentResolver
   * @param contentURI
   * @return path of image. Null if not found.
   */
  public static String getRealImagePathFromURI(ContentResolver contentResolver, Uri contentURI) {
    Cursor cursor = contentResolver.query(contentURI, null, null, null, null);
    if (cursor == null)
      return contentURI.getPath();
    else {
      cursor.moveToFirst();
      int idx = cursor.getColumnIndex(Images.ImageColumns.DATA);
      try {
        return cursor.getString(idx);
      } catch (Exception exception) {
        return null;
      }
    }
  }

}

Related Tutorials