gpt4 book ai didi

java - Base64 编码在 Android 中花费的时间太长

转载 作者:太空宇宙 更新时间:2023-11-03 11:04:52 27 4
gpt4 key购买 nike

我正在用相机拍摄图像。我将文件保存在公共(public)照片目录中,并将 Uri 保存到该文件。

我想将图像保存在 Base64 String 中,然后将其放在 HashMap 中,稍后再将其放入 XML 文件中。

protected Void doInBackground(Void...voids) {
options.inJustDecodeBounds = false;
//Bitmap bmp = BitmapFactory.decodeFile(imageFilePath,options);
InputStream in = null;
try {
in = getContentResolver().openInputStream(Uri.parse(mCurrentPhotoPath));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
options.inSampleSize = 2;
Bitmap image = BitmapFactory.decodeStream(in,null,options);
int imgHeight = image.getHeight();
int imgWidth = image.getWidth();
while(imgHeight>2000){
imgHeight = imgHeight / 2;
}
while(imgWidth>2000){
imgWidth = imgWidth / 2;
}

Bitmap test = Bitmap.createScaledBitmap(image,imgWidth,imgHeight,false);

String stest = base64EncodeDecode.encodeToBase64(test);


items.put("image",base64EncodeDecode.encodeToBase64(test);
return null;
}

Base64 编码时间过长。encodeToBase64方法

public String encodeToBase64(Bitmap image) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();

return Base64.encodeToString(b, Base64.DEFAULT);
}

如果我在编码时做错了什么,你能告诉我吗?

我希望我的问题很清楚。

亲切的问候!

最佳答案

如果你得到 !!! FAILED BINDER TRANSACTION !!! 错误可能是因为您将大量数据传递给另一个 Activity,您可以发送的数据量有限制。尝试将图像压缩到 50% 或 30% image.compress(Bitmap.CompressFormat.JPEG, 50, baos);

关于java - Base64 编码在 Android 中花费的时间太长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34920598/

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