0

I am very new to android development and I am trying to get the image exif data selected from gallery. I am able to open the gallery and choose an image. But as I see online I need to get the image path which I am not able to get as

cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

is returning 0.

This is my code

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_PICTURE) {
        Uri selectedImageUri = data.getData();
        selectedImagePath = getRealPathFromURI(this,selectedImageUri);
    }
}
}

My function to get the real path is this

public static String getRealPathFromURI_API11to18(Context context, Uri contentUri) {
    String[] proj = { MediaStore.Images.Media.DATA };
    String result = null;

    CursorLoader cursorLoader = new CursorLoader(
            context,
            contentUri, proj, null, null, null);
    Cursor cursor = cursorLoader.loadInBackground();

    if(cursor != null){
        int column_index =
                cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        result = cursor.getString(column_index);
    }
    return result;
}

android {
compileSdkVersion 25
buildToolsVersion "26.0.0"
defaultConfig {
    applicationId "*****"
    minSdkVersion 15
    targetSdkVersion 25
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner ""
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

}

Am I missing something?

7
  • I which android version you are trying , please also specify model. Commented Jun 27, 2017 at 10:50
  • I added my android target version etc. Commented Jun 27, 2017 at 10:54
  • what do you need any real image path for? Commented Jun 27, 2017 at 10:57
  • I need to display the exif data of the image and maybe try to update them. The links I read said that I should get the image path. the real one. Commented Jun 27, 2017 at 10:59
  • 1
    then use ExifInterface(FileDescriptor fileDescriptor) or ExifInterface(InputStream inputStream) constructors, or even better see @CommonsWare answer below Commented Jun 27, 2017 at 11:02

2 Answers 2

1

But as I see online I need to get the image path

Wherever you are going "online" is incorrect. For starters, there is no "image path".

My function to get the real path is this

That code has never been a good solution, has never worked well, and will fail for many Uri values.

I am trying to get the image exif data selected from gallery

Step #1: Add the support-exifinterface library to your project

Step #2: Call getContentResolver().openInputStream(selectedImageUri) to get an InputStream on the content identified by the Uri

Step #3: Pass that InputStream to the android.support.media.ExifInterface constructor

Step #4: Use that ExifInterface to get at your EXIF headers

Sign up to request clarification or add additional context in comments.

Comments

0

try this :

 public static String getRealPathFromURI_API19(Context context, Uri uri){
        String filePath = "";
        String wholeID = DocumentsContract.getDocumentId(uri);

         // Split at colon, use second item in the array
         String id = wholeID.split(":")[1];

         String[] column = { MediaStore.Images.Media.DATA };     

         // where id is equal to             
         String sel = MediaStore.Images.Media._ID + "=?";

         Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
                                   column, sel, new String[]{ id }, null);

         int columnIndex = cursor.getColumnIndex(column[0]);

         if (cursor.moveToFirst()) {
             filePath = cursor.getString(columnIndex);
         }   
         cursor.close();
         return filePath;
    }


    @SuppressLint("NewApi")
    public static String getRealPathFromURI_API11to18(Context context, Uri contentUri) {
          String[] proj = { MediaStore.Images.Media.DATA };
          String result = null;

          CursorLoader cursorLoader = new CursorLoader(
                  context, 
            contentUri, proj, null, null, null);        
          Cursor cursor = cursorLoader.loadInBackground();

          if(cursor != null){
           int column_index = 
             cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
           cursor.moveToFirst();
           result = cursor.getString(column_index);
          }
          return result;  
    }

    public static String getRealPathFromURI_BelowAPI11(Context context, Uri contentUri){
               String[] proj = { MediaStore.Images.Media.DATA };
               Cursor 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);
    }

2 Comments

This doesn't work. I have already tried this one. But thanks. :)
are you looking to get path of image from gallery {local}? and what does it mean ===But as I see online I need to get the image path which I am not able to get as

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.