【问题标题】:Image resolution or size issue gives 'Force Close' error图像分辨率或尺寸问题导致“强制关闭”错误
【发布时间】:2013-12-29 16:01:04
【问题描述】:

我有 2 个imageViewgallery 导入图像并设置在这些图像视图上。我基本上是这样做的:

String mPicPath1, mPicPath2;

protected void onCreate(Bundle icicle) {

mPicPath1 = null;
mPicPath2 = null; 

}

@Override
protected void onActivityResult(int requestCode, int resultcode, Intent data){
super.onActivityResult(requestCode, resultcode, data);

switch(requestCode){
case 1:
    if (data != null && resultcode == RESULT_OK) 
    {              

        Uri selectedImage = data.getData();

        String[] filePathColumn = {MediaStore.Images.Media.DATA};
        Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        mPicPath1 = cursor.getString(columnIndex);
        cursor.close();

        logoview.setBackgroundResource(0);
        logoview.setImageBitmap(BitmapFactory.decodeFile(mPicPath1));
    }
break;  
case 2:
    if (data != null && resultcode == RESULT_OK) 
    {              

        Uri selectedImage = data.getData();                     
        String[] filePathColumn = {MediaStore.Images.Media.DATA};
        Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        cursor.moveToFirst();                   
        mPicPath2 = cursor.getString(columnIndex);
        cursor.close();
        qrcodeview.setBackgroundResource(0);
        qrcodeview.setImageBitmap(BitmapFactory.decodeFile(mPicPath2));
    }
break;
}

我使用按钮onClickListener 启动意图并转到SecondActivity

save=(Button)findViewById(R.id.save);
save.setOnClickListener(new View.OnClickListener() {

@Override   
public void onClick(View v) {

Intent intent = new Intent(NewCard.this, Template.class);


    if (!TextUtils.isEmpty(mPicPath1)) {
        intent.putExtra("picture_path1", PicPath1);
    }
    if (!TextUtils.isEmpty(mPicPath2)) {
       intent.putExtra("picture_path2", PicPath2);
    }
    startActivity(intent);

}
});

还有我的SecondActivity 在两个不同的imageViews 上设置图像:

String pre_img_path1= getIntent().getStringExtra("picture_path1");
ImageView crdlogoframe = (ImageView)       findViewById(R.id.crdlogoframe);
crdlogoframe.setImageBitmap(BitmapFactory.decodeFile(pre_img_path1));

String pre_img_path2= getIntent().getStringExtra("picture_path2");
ImageView crdqrframe = (ImageView) findViewById(R.id.crdqrframe);
crdqrframe.setImageBitmap(BitmapFactory.decodeFile(pre_img_path2));

所以我的问题是关于图像的文件大小或分辨率。如果我从图库中拍摄 2 张​​高分辨率图像(由标准相机拍摄:1992kb,3264x2448)并单击我的保存(save.onClickListener)按钮,我会收到 Force Close 错误。如果我拍摄小尺寸图像没有问题(74kb,800x600),我可以继续SecondActivity 并查看图像已设置。我该如何解决这个问题。我应该在选择或设置时使用语法来调整图像大小吗?格式均为.Jpeg。非常感谢。

【问题讨论】:

  • 文件大小不相关。默认情况下,使用的内存是宽 x 高 x 4 字节。所以,3264x2488x4 = 大约 31MB。您将不得不在加载图像时缩放图像或降低图像分辨率。 SO上已经有很多问题和答案可以做到这一点。
  • 但我没有使用相机意图拍照。我只是挑选拍摄的图像。你能检查我的代码并更实际地建议我吗?
  • @Simon 它是 1992Kb 和 2 张图片大约 4MB。
  • 请再次阅读我的评论。文件大小不相关。图像在存储时被压缩,在加载时被解压缩。所需内存如我所说,宽 x 高 * 4 字节,因为默认情况下,每个像素需要 4 字节(32 位颜色深度)

标签: android image bitmap decode


【解决方案1】:

我还没有代表发表评论所以....但是正如西蒙所说,您的图像太大了 31MB 需要尝试将内容保持在 16MB 以下,因此您必须重新调整它们的大小才能显示它们

【讨论】:

  • 没有办法31MB。正如我在评论中提到的那样,它是 1992Kb,两张图片大约是 4MB。这足够大,不会显示在我的应用中。
  • 1992kb 可能是压缩后的文件大小,与显示图像所需的内存量不同。正如西蒙所说......再次......宽 X 高 X 4 会让你接近显示它所需的内存
  • 这样说吧。我可以创建在磁盘上占用 100K 并且需要 2 MB RAM 来显示的文件,或者我可以创建一个在磁盘上占用 2MB 并且需要 2MB 来显示的文件。再一次,文件大小无关紧要。
【解决方案2】:

代替

BitmapFactory.decodeFile(mPicPath2)

你需要使用类似的东西

BitmapFactory.decodeFile(mPicPath2, options)

其中 options 是 BitmapFactory.Options 的一个实例,inSampleSize 设置为类似 4 的值,它将在加载图像时缩小图像。

【讨论】:

  • 首先谢谢,我试过了,但对我没有帮助。也许是 mPicPath1 的 bec 而 mPicPath2 是字符串值?
猜你喜欢
  • 2011-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-04
  • 1970-01-01
相关资源
最近更新 更多