3

I am using the standard android pick images from gallery way of picking images from phone.. My same code works perfectly fine on all android apart from android 5.0 and above.

I did some debugging and the problem seems to be below:

public String getPath (Uri uri) {

    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = getContentResolver().query(uri, projection, null, null, null); 
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

    cursor.moveToFirst();
    ImagePath=  cursor.getString(column_index) ;

    System.out.println("HERE" + ImagePath);   // returns null
    return cursor.getString(column_index);
 }

The prinln I did returns null fOR ImagePath.. this where the problem is.. It does not return null on any other android apart from 5.0+.. How can i get this to work?

Community
  • 1
  • 1
joshua
  • 67
  • 1
  • 7
  • I've also noticed this. It seems Android 5.0 broke the DATA column. It would be good to get an answer that is a real fix for this .... – James Apr 28 '15 at 11:16

3 Answers3

0

try:

String imagePath = cursor.getString(cursor.getColumnIndex(projection[0]));
Robert
  • 5,278
  • 43
  • 65
  • 115
Harry
  • 1
0

You should use startActivityForResult and onActivityResult. Tricks:

    Intent pickPicIntent = new Intent();
    // pickPicIntent.setDataAndType(
    // MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
    pickPicIntent.setType("image/*");
    pickPicIntent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(pickPicIntent, CODE_PICK_PICTURE);

And then:

    Bundle extras = data.getExtras();
    if (extras != null) {
        Bitmap photo = extras.getParcelable("data");
        // civAvatar.setImageBitmap(photo); 
        FileOutputStream fos = null;
        try {
            // Store Bitmap into a File
            fos = new FileOutputStream(AVATAR_FILE);
            photo.compress(Bitmap.CompressFormat.PNG, 100, fos);
            AVATAR_FILE_TMP.deleteOnExit();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            IoUtils.closeSilently(fos);
            finish();
        }
    }

Hope you are inspired.

SilentKnight
  • 13,761
  • 19
  • 49
  • 78
0

maybe you could be interested in RxPaparazzo . This library supports to API 24 (Android 7) and allows you take images from camera, gallery, file system or even remote images (for instance, from Google Photos or Google Drive)

The usage is something like this:

RxPaparazzo.takeImage(activityOrFragment)
    .usingCamera()  // or .usingGallery()
    .subscribe(response -> {
        if (response.resultCode() != RESULT_OK) {
            response.targetUI().showUserCanceled();
            return;
        }

        response.targetUI().loadImage(response.data());
    });
Miguel Garcia
  • 1,389
  • 16
  • 16