Selecting image from gallery not working on Redmi Note 4

两盒软妹~` 提交于 2019-12-01 22:14:12

问题


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.


回答1:


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.




回答2:


I am facing the similar issue in Redmi 5A. And solve the problem using following way.

Add below entities in your manifest file

android:hardwareAccelerated="false"

android:largeHeap="true"

It will work in some environments.

Refer answer at https://stackoverflow.com/a/32245018/2706551




回答3:


The reason is that you get file:// uri back. not content:// so content resolver doesn't work.



来源:https://stackoverflow.com/questions/47977914/selecting-image-from-gallery-not-working-on-redmi-note-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!