gpt4 book ai didi

java - Android Imageview 上的大图像因 OOM 错误导致应用程序崩溃

转载 作者:行者123 更新时间:2023-12-02 09:07:05 24 4
gpt4 key购买 nike

下面的代码允许用户从他们的图库中选择 4 张图像并上传到服务器。目前它正在上传所有 4 个小图像,但是当我尝试大图像时,它会因 OOM 错误而崩溃。

即使我尝试加载 2 个大图像,当我加载 OOM 错误的大图像时,应用程序也会崩溃。处理此类问题最有效的方法是什么?我读过这篇Android Bitmaps但我似乎不知道如何在我的四个图像代码中实现。我尝试过 Picasso,但该应用程序的行为仍然与上面相同(同上)。


SelectImageGallery1 = (Button)findViewById(R.id.buttonSelect1);
SelectImageGallery2 = (Button)findViewById(R.id.buttonSelect2);
SelectImageGallery3 = (Button)findViewById(R.id.buttonSelect3);
SelectImageGallery4 = (Button)findViewById(R.id.buttonSelect4);

UploadImageServer = (Button)findViewById(R.id.buttonUpload);

SelectImageGallery1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Image1 From Gallery"), 1);

}
});

SelectImageGallery2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Image4 From Gallery"), 2);
}
});

SelectImageGallery3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Image3 From Gallery"), 3);

}
});

SelectImageGallery4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Image4 From Gallery"), 4);

}
});

UploadImageServer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
GetImageNameEditText1 = imageName1.getText().toString();
GetImageNameEditText2 = imageName2.getText().toString();
GetImageNameEditText3 = imageName3.getText().toString();
GetImageNameEditText4 = imageName4.getText().toString();
ImageUploadToServerFunction();
}
});

}



@Override
protected void onActivityResult(int RC, int RQC, Intent I) {
super.onActivityResult(RC, RQC, I);
if (RC == 1&&RQC == RESULT_OK&&I != null&&I.getData() != null) {
Uri uri = I.getData();
try {
bitmap1 = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
imageView1.setImageBitmap(bitmap1);
} catch (IOException e) {
e.printStackTrace();
}
}

if (RC == 2 && RQC == RESULT_OK && I != null && I.getData() != null) {
Uri uri = I.getData();
try {
bitmap2 = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
//bitmap1 = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
imageView2.setImageBitmap(bitmap2);
} catch (IOException e) {
e.printStackTrace();
}
}

if (RC == 3 && RQC == RESULT_OK && I != null && I.getData() != null) {
Uri uri = I.getData();
byte[] imageAsBytes=null;
try {
bitmap3 = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
//bitmap1 = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
imageView3.setImageBitmap(bitmap3);
} catch (IOException e) {
e.printStackTrace();
}
}
if (RC == 4 && RQC == RESULT_OK && I != null && I.getData() != null) {
Uri uri = I.getData();
try {
bitmap4 = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
//bitmap1 = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
imageView4.setImageBitmap(bitmap4);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public String getStringImage1(Bitmap bitmap1){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap1.compress(Bitmap.CompressFormat.JPEG, 60, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage1 = Base64.encodeToString(imageBytes, Base64.DEFAULT);
return encodedImage1;
}

public String getStringImage2(Bitmap bitmap2){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap2.compress(Bitmap.CompressFormat.JPEG, 60, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage2 = Base64.encodeToString(imageBytes, Base64.DEFAULT);
return encodedImage2;
}
public String getStringImage3(Bitmap bitmap3){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap3.compress(Bitmap.CompressFormat.JPEG, 60, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage3 = Base64.encodeToString(imageBytes, Base64.DEFAULT);
return encodedImage3;
}

public String getStringImage4(Bitmap bitmap4){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap4.compress(Bitmap.CompressFormat.JPEG, 60, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage4 = Base64.encodeToString(imageBytes, Base64.DEFAULT);
return encodedImage4;
}

最佳答案

这是通过使用 Glide 实现的,如下所示。使用 glide 可确保处理 OOM 错误,并且在加载 30mb 图像时不会重复出现这些错误。

 @Override
protected void onActivityResult(int RC, int RQC, Intent I) {
super.onActivityResult(RC, RQC, I);
if (RC == 1 && RQC == RESULT_OK && I != null && I.getData() != null) {
Uri uri = I.getData();
RequestOptions options = new RequestOptions()
.format(DecodeFormat.PREFER_ARGB_8888)
.placeholder(R.drawable.ic_launcher_background)
.error(R.drawable.ic_launcher_background);

Glide.with(this)
.setDefaultRequestOptions(options)
.asBitmap()
.load(uri)
.centerInside()
.into(new CustomTarget<Bitmap>(512, 512) {
@Override
public void onResourceReady(@NonNull Bitmap bitmap1, @Nullable Transition<? super Bitmap> transition) {
imageView1.setImageBitmap(bitmap1);
UploadActivity.this.bitmap1 = bitmap1;
}

@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
}
});
}

关于java - Android Imageview 上的大图像因 OOM 错误导致应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59725379/

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