尝试从文件加载图像,改为使用黑色圆圈

时间:2016-07-20 20:36:04

标签: android

我正在尝试从手机的文件中加载图片。当文件是一个小图像时它很顺利:

40kb图片的结果,275x269

但是当我尝试加载更大的图像时,会发生这种情况:

2.63MB图片的结果,4160x2340

我的代码 - 获取文件:

private void selectImage(){
    Intent i = new Intent();
    i.setType("image/*");
    i.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(i, "Select File"), SELECT_FILE);
}

将图像设置为ImageView的背景:

public void onActivityResult(int resultCode, Intent data){
    super.onActivityResult(resultCode, data);
    if (resultCode == Activity.RESULT_OK) {
            if(data!=null) {
                try {
                    photo = MediaStore.Images.Media
                            .getBitmap(getApplicationContext().getContentResolver(),
                            data.getData());
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            photo.compress(Bitmap.CompressFormat.PNG, 100, baos);

            imageView.setImageBitmap(photo);
        }
    }
}

这是Android Monitor向我展示的内容:

07-20 17:32:57.949 3200-3200/rafael.couto.newmoka D/dalvikvm: GC_FOR_ALLOC freed 265K, 6% free 5668K/5972K, paused 4ms, total 6ms
07-20 17:32:58.079 3200-3200/rafael.couto.newmoka I/dalvikvm-heap: Grow heap (frag case) to 42.701MB for 38937612-byte allocation
07-20 17:32:58.079 3200-3208/rafael.couto.newmoka D/dalvikvm: GC_FOR_ALLOC freed 2K, 1% free 43691K/44000K, paused 5ms, total 5ms
07-20 17:32:59.599 3200-3200/rafael.couto.newmoka D/dalvikvm: GC_FOR_ALLOC freed 2120K, 6% free 45599K/48036K, paused 4ms, total 4ms
07-20 17:33:00.889 3200-3200/rafael.couto.newmoka D/dalvikvm: GC_FOR_ALLOC freed 2043K, 9% free 47650K/52132K, paused 4ms, total 4ms
07-20 17:33:00.899 3200-3200/rafael.couto.newmoka I/dalvikvm-heap: Grow heap (frag case) to 54.569MB for 8392804-byte allocation
07-20 17:33:00.909 3200-3208/rafael.couto.newmoka D/dalvikvm: GC_FOR_ALLOC freed 0K, 8% free 55846K/60332K, paused 4ms, total 4ms
07-20 17:33:03.209 3200-3200/rafael.couto.newmoka D/dalvikvm: GC_FOR_ALLOC freed 4094K, 15% free 51752K/60332K, paused 3ms, total 3ms
07-20 17:33:03.239 3200-3200/rafael.couto.newmoka I/dalvikvm-heap: Grow heap (frag case) to 66.586MB for 16793700-byte allocation
07-20 17:33:03.249 3200-3208/rafael.couto.newmoka D/dalvikvm: GC_FOR_ALLOC freed 0K, 12% free 68152K/76736K, paused 8ms, total 8ms
07-20 17:33:03.589 3200-3200/rafael.couto.newmoka D/dalvikvm: GC_FOR_ALLOC freed 8200K, 22% free 59952K/76736K, paused 2ms, total 2ms
07-20 17:33:03.589 3200-3200/rafael.couto.newmoka I/dalvikvm-heap: Grow heap (frag case) to 67.363MB for 9210496-byte allocation
07-20 17:33:03.599 3200-3208/rafael.couto.newmoka D/dalvikvm: GC_FOR_ALLOC freed 156K, 11% free 68790K/76736K, paused 7ms, total 7ms

发生了什么,我该怎么办?

2 个答案:

答案 0 :(得分:1)

你几乎已经在你的问题中发现了这个问题 - 问题在于使用占用大量内存的高分辨率图像。您需要将图像分辨率采样到您实际需要的大小 - 但看起来您已经拥有了一个较小的图像。如果您想查看缩小图像,请查看https://developer.android.com/training/displaying-bitmaps/load-bitmap.html

答案 1 :(得分:0)

(代表OP发布)

问题解决了,感谢peteyucsunil。我这样做是为了防止大图像:

public void onActivityResult(int resultCode, Intent data){
    super.onActivityResult(resultCode, data);
    if (resultCode == Activity.RESULT_OK) {
        if(data!=null) {
            try {
                photo = MediaStore.Images.Media
                            .getBitmap(getApplicationContext().getContentResolver(),
                                    data.getData());

                    int width = bigPhoto.getWidth();
                    int height = bigPhoto.getHeight();

                    while(width>=500 || height>=500){
                        photo = Bitmap.createScaledBitmap(bigPhoto, (int)(width*0.4), (int)(height*0.4), true);
                        width = photo.getWidth();
                        height = photo.getHeight();
                    }
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        photo.compress(Bitmap.CompressFormat.PNG, 100, baos);

        imageView.setImageBitmap(photo);
    }
}