【发布时间】:2013-12-29 16:01:04
【问题描述】:
我有 2 个imageView 从gallery 导入图像并设置在这些图像视图上。我基本上是这样做的:
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