gpt4 book ai didi

android - 图像太大会导致应用程序返回到以前的 Activity 吗?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:59:31 26 4
gpt4 key购买 nike

AddMoreClaims Activity 中,有一个imageViewbutton 和save button

单击按钮时,它将转到 activeGallery() 并让用户从图库中选择图像。然后,所选图像将显示在 imageView AddMoreClaims 上。

  private void activeGallery() {
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RESULT_LOAD_IMAGE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RESULT_LOAD_IMAGE:
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK & null != data) {
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]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
photo = decodeSampledBitmapFromUri(picturePath,200,200); // make image clear
imageView.setImageBitmap(photo); // display image on imageView
}
break;
}

public Bitmap decodeSampledBitmapFromUri(String path, int reqWidth,
int reqHeight) {

Bitmap bm = null;
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);

// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth,
reqHeight);

// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
bm = BitmapFactory.decodeFile(path, options);
return bm;
}

public int calculateInSampleSize(

BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;

if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;

// Calculate the largest inSampleSize value that is a power of 2
// and keeps both height and width larger than the requested
// height and width.
while ((halfHeight / inSampleSize) > reqHeight &&
(halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
}

为了确保放置在 imageView 上的所选图像清晰,我添加了 decodeSampledBitmapFromUri(picturePath,200,200)到目前为止一切正常。

当点击保存按钮时,它假设将图像返回到AddClaims listView

 saveBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent returnIntent = new Intent(); // back to AddClaims
returnIntent.putExtra("photo", photo);
setResult(Activity.RESULT_OK, returnIntent);
finish();

}
});

但是,上述编码有时不起作用。我不确定是不是因为图像选择太大,当我点击保存按钮并想将图像返回到 listView AddClaims 时,它只是返回到 AddClaims 之前的 Activity 。 。但是代码适用于某些选定的图像。为什么会这样?

最佳答案

这不是通过返回 Intent 传递图像的好方法。尝试将其写入 sd 卡,从其他屏幕访问。(如果它是 secret ,则将其删除)

图像位图的大小范围通常为 18MB+...因此取决于堆的可用性及其可能有效或无效。即使在高端设备中,这也可能由于堆中的空间不可用而发生。或者您可以在执行此操作之前以编程方式从堆中清除不需要的项目。

关于android - 图像太大会导致应用程序返回到以前的 Activity 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34315387/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com