从图库中选择图像在Redmi Note 4上不起作用 [英] Selecting image from gallery not working on Redmi Note 4

查看:117
本文介绍了从图库中选择图像在Redmi Note 4上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在S/O上还看到了其他一些与此相关的问题,但是与我的问题最接近的问题似乎并没有得到很多答复(

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.

我已经在另外两台设备(Moto E和称为Coolpad的设备)上对此进行了测试,它们似乎都可以正常工作.

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

(调试Android源代码似乎不是一个可行的选择.)

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

在主活动中,在UI触发器上,我使用以下代码启动图库选择器:

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);
    }

我这样处理结果:

@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();
    }
}

一旦我将文件路径存储在全局变量mCurrentPhotoPath中,就会调用以下方法:

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);
}

即使在Redmi上,直到现在,一切都进行得很顺利.在UploadActivity的onCreate方法上,我成功接收到图像文件路径字符串.

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);
}

现在我已经尝试调试此问题,但是一旦我进入BitmapFactory的源代码,就会遇到Android Studio似乎无法解决的问题(

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.

推荐答案

因此,我能够将问题缩小为错误的文件路径. Uri到文件路径功能在所有设备上均未获得理想的结果.

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.

我使用了此处建议的方法: https://stackoverflow.com/a/7265235/3438497

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

并稍稍调整了代码,以便从图库中选择图像时,我直接使用图像的Uri.

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

这篇关于从图库中选择图像在Redmi Note 4上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆