4

I recently fixed a known bug in my app that occurs on some devices; when the user takes a photo from an intent launched from my app, in the onActivityResult Uri uri = intent.getData(); returns null. I managed to fix that in the suggested manner. However I get the exact same issue when the user needs to Select a picture from his phone's photo gallery. The same intent.getData() == null.

Starting the intent:

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "Select Picture"),     
R.id.SELECT_IMAGE_ACTIVITY_REQUEST_CODE);

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 filePath = cursor.getString(columnIndex);
cursor.close();
dataHasChanged(ACTION_PICTURE, filePath);

PS: Strangely enough if I start the intent like shown below I can get the intent.getData() but only if I use the "Gallery" app on my samsung s4 and not the GooglePhotos app.

Intent pickImageIntent = new Intent(
    Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
if (pickImageIntent.resolveActivity(getPackageManager()) != null)
    startActivityForResult(pickImageIntent, R.id.SELECT_IMAGE_ACTIVITY_REQUEST_CODE);

I am unsure on how to proceed. I find the entire Android intents affair very confusing sometimes.

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
Sebek
  • 642
  • 8
  • 22

2 Answers2

1

I'm giving you code for your reference:

Use following code after clicking pic from your app:

    Intent intent = new Intent();
    // call android default gallery
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    try {
        intent.putExtra("return-data", true);
        startActivityForResult(Intent.createChooser(intent,"Complete action using"), PICK_FROM_GALLERY);
    } catch (ActivityNotFoundException e) {
        // Do nothing for now
    }

Below code will be in your onActivityResult():

        if (requestCode == PICK_FROM_GALLERY) {
            if (resultCode != RESULT_CANCELED) {
                Bundle extras2 = data.getExtras();
                if (extras2 != null) {
                photo = extras2.getParcelable("data");
                bitmap = photo;
                profile_imageView.setImageBitmap(photo);
                new ImageUploadTask().execute();
                }   
            }
        }

Hope this will solve your problem.

Sagar
  • 23,903
  • 4
  • 62
  • 62
VVB
  • 7,363
  • 7
  • 49
  • 83
  • This works great if there was only one ImageView to set the img to... what if I want to use the same logic to load file for more than one ImageView? how does the code on onActivityResult knows which ImageView to set the photo to? Any tips? – Vu Nguyen Dec 12 '14 at 08:18
-1
static final int REQUEST_GALLERY_IMAGE = 14;

----------
 Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
        photoPickerIntent.setType("image/*");
        startActivityForResult(photoPickerIntent, REQUEST_GALLERY_IMAGE );


 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      if (requestCode == REQUEST_GALLERY_IMAGE && resultCode == RESULT_OK) {
            try {
                final Uri imageUri = data.getData();
                InputStream imageStream = getContentResolver().openInputStream(imageUri);
                final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
                imagePreview.setImageBitmap(selectedImage);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
 }