gpt4 book ai didi

java - 为什么我会收到 TransactionTooLargeException?

转载 作者:行者123 更新时间:2023-12-01 17:51:34 25 4
gpt4 key购买 nike

我正在开发一个 Android 项目,但当我需要从图库中拍照时,我遇到了困难。确实,当我拍摄第一张照片时,一切都很顺利。但是,如果我拍摄第二张照片,则会引发异常“TransactionTooLargeException”并且我的应用程序崩溃。

用于启动 Activity 的代码:

public void addImage(View view) {
if(isPermissionEnable) {
try {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, ""), ADD_IMAGE);
} catch (Exception ex) {
GraphicUtils.displayPopup(this, getString(R.string.warning),
getString(R.string.image_too_large));
}
}else
{
askPermissions();
}

}

获取结果的代码:

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

if (resultCode == RESULT_OK) {
switch (requestCode) {
case ADD_IMAGE:
if(getContentResolver().getType(data.getData()).contains("jpeg") || getContentResolver().getType(data.getData()).contains("jpg") || getContentResolver().getType(data.getData()).contains("png") || getContentResolver().getType(data.getData()).contains("bmp")) {

Uri uri = data.getData();

try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
int minimumSize = (bitmap.getWidth() < bitmap.getHeight()) ? bitmap.getWidth() : bitmap.getHeight();
finalBitmap = ThumbnailUtils.extractThumbnail(bitmap, minimumSize, minimumSize);
imageView.setImageBitmap(finalBitmap);
button_right.setVisibility(View.VISIBLE);
button_left.setVisibility(View.VISIBLE);

} catch (Exception e) {
e.printStackTrace();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(uri.toString());
builder.setTitle("Erreur");
AlertDialog dialog = builder.create();
dialog.show();
}
}
else{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(getContentResolver().getType(data.getData()));
builder.setTitle("Erreur");
AlertDialog dialog = builder.create();
dialog.show();
}
break;
default :
break;
}
}
}

请问您有什么办法可以解决我的问题吗?

最佳答案

来自dcoumentation :

The key to avoiding TransactionTooLargeException is to keep all transactions relatively small. Try to minimize the amount of memory needed to create a Parcel for the arguments and the return value of the remote procedure call. Avoid transferring huge arrays of strings or large bitmaps. If possible, try to break up big requests into smaller pieces.

我猜您正在执行几笔交易,或者很可能是一笔相当大的交易。

尝试在您的应用程序中分析和评估哪些地方可以限制和遏制您请求的数据量。

另外,看看这个 SO question了解更多信息。

关于java - 为什么我会收到 TransactionTooLargeException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60785978/

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