androidredmi-devicexiaomi

Selecting image from gallery not working on Redmi Note 4


I have seen a couple of other questions on S/O related to this, but the one that was closest to my issue doesn't seem to have got many responses (Xiaomi MI device not picking image from Gallery). Hoping this question will have better luck.

I am trying to select an image from the gallery of the phone, and pass the image path to another activity where it get previewed for the user.

I've tested this on two other devices (Moto E and something called Coolpad?) and they both seem to be working just fine.

(Debugging Android source code doesn't seem to be a practical option.)

In the main activity, on a UI trigger, I launch the gallery picker with the following code:

private void dispatchPickPictureIntent() {
        Intent pickPictureIntent = new Intent(Intent.ACTION_PICK);
        pickPictureIntent.setType("image/*");
        startActivityForResult(pickPictureIntent, REQUEST_IMAGE_PICK);
    }

I handle the result like so:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_PICK && resultCode == RESULT_OK) {
        Uri selectedImage = data.getData();
        mCurrentPhotoPath = getRealPathFromURI(this, selectedImage);
        launchUploadActivity();
    }
}

private String getRealPathFromURI(Context context, Uri uri) {
    Cursor cursor = null;
    try {
        String [] proj = {MediaStore.Images.Media.DATA};
        cursor = context.getContentResolver().query(uri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } finally {
        if (cursor != null)
            cursor.close();
    }
}

Once I have got the file path stored in the global variable mCurrentPhotoPath the following method gets called:

private void launchUploadActivity() {
    Intent intent = new Intent(HomeActivity.this, UploadActivity.class);
    intent.putExtra(getString(R.string.photo_path), mCurrentPhotoPath);
    startActivityForResult(intent, REQUEST_IMAGE_UPLOAD);
}

Even on the Redmi, up until here, things run smoothly. On the onCreate method of the UploadActivity, I receive the image file path string successfully.

But, then, I try to preview the image:

private void previewPhoto() {
    imageView.setVisibility(View.VISIBLE);
    BitmapFactory.Options options = new BitmapFactory.Options();

    // Avoid OutOfMemory Exception
    options.inSampleSize = 8;

    // This line returns a null, only on the Xiaomi device
    final Bitmap bitmap = BitmapFactory.decodeFile(photopath, options);

    imageView.setImageBitmap(bitmap);
}

Now I have tried to debug this issue, but once I step into BitmapFactory's source code, I run in to a seemingly unresolved issue with Android Studio (https://stackoverflow.com/a/40566459/3438497) which renders it useless.

Any pointers on how I can proceed from here would be greatly appreciated.


Solution

  • So I was able to narrow down the issue to an incorrect file path. The Uri to filepath function did not get the desired result on all devices.

    I used the approach suggested here:https://stackoverflow.com/a/7265235/3438497

    and tweaked the code a little bit so that when picking images from gallery, I use the Uri of the image directly.