3

I'am invoking default gallery app from my app to select any photo. Below is my code to get the selected image path from gallery. It's working fine for all the photos except few. When i select any of PICASA uploaded photos from Gallery, app is force closing. Please help me.


Inside onActivityResult()....

            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };
            Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String selectedPhotoPath = cursor.getString(columnIndex).trim();  <<--- NullPointerException here
            cursor.close(); 
            bitmap = BitmapFactory.decodeFile(selectedPhotoPath);
            ......      
Santhosh
  • 4,956
  • 12
  • 62
  • 90

4 Answers4

7

Sometimes data.getData(); returns null depending on the app you use to get the picture. A workaround for this is to use the above code in onActivityResult:

/**
*Retrieves the path of the image that was chosen from the intent of getting photos from the galery
*/
Uri selectedImageUri = data.getData();

// OI FILE Manager
String filemanagerstring = selectedImageUri.getPath();

// MEDIA GALLERY
String filename = getImagePath(selectedImageUri);

String chosenPath;

if (filename != null) {

   chosenPath = filename;
} else {

   chosenPath = filemanagerstring;
}

The variable chosenPath will have the correct path of the chosen image. The method getImagePath() is this:

public String getImagePath(Uri uri) {
    String selectedImagePath;
    // 1:MEDIA GALLERY --- query from MediaStore.Images.Media.DATA
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    if (cursor != null) {
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        selectedImagePath = cursor.getString(column_index);
    } else {
        selectedImagePath = null;
    }

    if (selectedImagePath == null) {
        // 2:OI FILE Manager --- call method: uri.getPath()
        selectedImagePath = uri.getPath();
    }
    return selectedImagePath;
}
AggelosK
  • 4,313
  • 2
  • 32
  • 37
2

Please try below code

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);

if (resultCode == RESULT_OK){
 Uri targetUri = data.getData();
 textTargetUri.setText(targetUri.toString());
 Bitmap bitmap;
 try {
  bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(targetUri));
  ....
 } catch (FileNotFoundException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
}
}

OR

Please check below link

OR

How to pick an image from gallery (SD Card) for my app?

Community
  • 1
  • 1
Nikhil
  • 16,194
  • 20
  • 64
  • 81
0
String ImagePath  = "";
private void setImageFromGallery(Intent data) {
    Uri selectedImage = data.getData();
    String[] filePathColumn = {MediaStore.Images.Media.DATA};
    Cursor cursor = this.getContentResolver().query(selectedImage, filePathColumn, null, null, null);
    if (cursor != null) {
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();
        Log.i("choosepath", "image" + picturePath);
        ImagePath = picturePath;
    } else {
        ImagePath = selectedImage.getPath();   // Add this line 
    }
   ImageView imgView = (ImageView) findViewById(R.id.imgView);
   imgView.setImageBitmap(BitmapFactory.decodeFile(imagePath));
    Bitmap bitmap = Utilities.rotateImage(pictureImagePath);
}
Vicky
  • 5,098
  • 2
  • 33
  • 31
0

Current Android system (first version -> 2.3.3 -> even 4.4.2) looks like not able to selected multiple files, so you need custom gallery to do that.

Afer researched so many times, I found Custom Camera Gallery library can help you do that thing already.

Huy Tower
  • 7,769
  • 16
  • 61
  • 86