【问题标题】:pick gallery image and convert to bitmap?选择画廊图像并转换为位图?
【发布时间】:2013-03-27 17:39:31
【问题描述】:

我正在尝试从图库中挑选一张图片,将其转换为 Bitmap,然后将其显示在 ImageView 中。

以下代码用于从图库中选择图片

    Intent pickPhoto = new Intent(
            Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(pickPhoto, 1);`

onActivityResult 中我将其转换为Bitmap 并在ImageView 中显示它

protected void onActivityResult(int requestCode, int resultCode,
        Intent imageReturnedIntent) {
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
    switch (requestCode) {
    case 1:
        if (resultCode == RESULT_OK) {
            Uri selectedImage = imageReturnedIntent.getData();
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 4;//returning null for below statement
            bitmap = BitmapFactory.decodeFile(selectedImage.toString(), options);
            productItemImage.setImageBitmap(bitmap);
        }
        break;
    }
}

所选图像Uri中的值返回为“content://media/external/images/media/3647”

productItemImage 是我的ImageView,我在其中显示Bitmap。没有错误,但位图返回 null。

请帮忙

【问题讨论】:

    标签: android bitmap android-imageview bitmapimage


    【解决方案1】:

    问题可能是您在onActivityResult() 函数中调用super.onActivityResult()

    这是我去年写的一个答案,它给出了你想要做什么的完整示例,并且允许你从相机和图库中进行选择:Allow user to select camera or gallery for image(这个答案得分很高,所以我认为对你有帮助)。

    【讨论】:

    猜你喜欢
    • 2023-01-07
    • 2020-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-20
    • 1970-01-01
    相关资源
    最近更新 更多