gpt4 book ai didi

解码位图时出现 Android OutOfMemoryError

转载 作者:行者123 更新时间:2023-11-30 00:22:06 26 4
gpt4 key购买 nike

大家好,我正在做一个在服务器上传图片文件的项目。我正在使用 Volley 向服务器发送数据。我将发送的数据包含 base64 字符串。我在解码图像文件时遇到了OutOfMemoryError。我是否正确实现了它?这是我的代码:

for (int i = 0; i < pathList.size(); i++) {
//error occurred here
Bitmap image = BitmapFactory.decodeFile(pathList.get(i), options);
Log.d("bitmapImage", "nisudSiya" + " " + image);
encodedImage = UploadImageHelper.encodeImageBitmap(UploadImageHelper.scaleBitmap(image, 1000, 1000));
stringUri.add(encodedImage);
}
StringRequest stringRequest = new StringRequest(Request.Method.POST, POST_URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(CreatePostActivity.this, response.toString(), Toast.LENGTH_SHORT).show();
VolleyApp.getInstance().cancelPendingRequests(PUSH_MULTIPLE_IMAGES);
mProgressUpload.dismiss();
finish();
Log.d("tyler", response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
Toast.makeText(CreatePostActivity.this, "Error Uploading!", Toast.LENGTH_SHORT).show();
mProgressUpload.dismiss();
Log.d("tagsdsad23", error.toString());
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {

for (int i = 0; i < stringUri.size(); i++) {
Attachment attachment = new Attachment();
attachment.setType("image");
attachment.setUrl(stringUri.get(i));
shit.add(attachment);
}
Log.d("shitsize", String.valueOf(shit.size()));
JSONArray jsonArray = new JSONArray();
for (int i = 0; i < shit.size(); i++) {
try {
JSONObject jsonObject = new JSONObject();
Log.d("shittype", shit.get(i).getUrl());
jsonObject.put("type", shit.get(i).getType());
jsonObject.put("url", shit.get(i).getUrl());
jsonArray.put(jsonObject);
Log.d("tyler-gwapa", jsonObject.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
Log.d("tyler-gwapa", jsonArray.toString());

Map<String, String> params = new HashMap<String, String>();
params.put(USER_TOKEN, userToken);
params.put(CAPTION, postContent.getText().toString());
params.put(ATTACHMENT, jsonArray.toString());
return params;
}
};
VolleyApp.getInstance().addToRequestQueue(stringRequest, PUSH_MULTIPLE_IMAGES_TAG);

最佳答案

尝试以下压缩位图并对其进行编码。

 Bitmap selectedImage = getResizedBitmap(yourBitmaptoCompress, 350, 350);

定义getResizedBitmap方法

 public Bitmap getResizedBitmap(Bitmap image, int bitmapWidth, int 
bitmapHeight) {
return Bitmap.createScaledBitmap(image, bitmapWidth, bitmapHeight,
true);
}

编码图像位图(base64)

 String   encodedImage = encodeImage(selectedImage);

定义encodeImage方法

 private String encodeImage(Bitmap bm) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 30, baos);
byte[] b = baos.toByteArray();
String encImage = Base64.encodeToString(b, Base64.DEFAULT);
return encImage;
}

关于解码位图时出现 Android OutOfMemoryError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46089819/

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