【问题标题】:Out of memory Error for camera images while converting bitmap into byte array将位图转换为字节数组时相机图像内存不足错误
【发布时间】:2017-02-10 06:12:04
【问题描述】:

在经历了很多内存分配错误解决方案之后,我为我的应用程序暗示并尝试了很多,但我不知道为什么它仍然会导致内存崩溃。它只发生在相机文件夹图像而不是其他人.如果某处有问题,请帮助我更正我的代码。 首先是使用图像意图选择图像后的 On Activity 结果:

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

        if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
            Uri filePath = data.getData();

            try {
                //getting image from gallery
                bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);

                //Setting image to ImageView
                ivImage.setImageBitmap(bitmap);
                Uri selectedImageUri = data.getData();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

另一种方法是通过将位图转换为字节数组来将图像上传到服务器:

public void upload()
{
     progressDialog = new ProgressDialog(AddPropertyThird.this);
                progressDialog.setMessage("Uploading, please wait...");
                progressDialog.show();

                //converting image to base64 string
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inJustDecodeBounds = false;
                options.inPreferredConfig = Bitmap.Config.RGB_565;
                options.inDither = true;

              ByteArrayOutputStream stream = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
                byte[] imageBytes = stream.toByteArray();
                bitmap.recycle();

                final String imageString = Base64.encodeToString(imageBytes, Base64.DEFAULT);

                //sending image to server
                StringRequest request = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String s) {
                        progressDialog.dismiss();

                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Log.e("volleyerror", "" + volleyError);
                    }
                }) {
                    //adding parameters to send
                    @Override
                    protected Map<String, String> getParams() throws AuthFailureError {
                        Map<String, String> parameters = new HashMap<String, String>();
                        parameters.put("ImgStr", imageString);

                        return parameters;
                    }
                };

                RequestQueue rQueue = Volley.newRequestQueue(AddPropertyThird.this);
                rQueue.add(request);
            }

这是我的 logcat 错误:

 E/dalvikvm-heap: Out of memory on a 4192272-byte allocation.
Out of memory on a 4186240-byte allocation.
 Out of memory on a 2089032-byte allocation.
 FATAL EXCEPTION: main
                                                                    java.lang.OutOfMemoryError
                                                                      java.io.ByteArrayOutputStream.toByteArray(ByteArrayOutputStream.java:122)

【问题讨论】:

标签: android


【解决方案1】:

对于第一个错误check out this question,了解如何在避免内存不足错误的同时加载图像。

您在使用 ByteArray 时遇到内存不足错误,因为您将图像从线上的流中拉入内存

byte[] imageBytes = stream.toByteArray();

因此,您应该避免这样做。 Check out this answer.

【讨论】:

  • 你能解释一下吗?实际上在链接中,他们使用了多部分进行图片上传,这不是我的情况。
猜你喜欢
  • 1970-01-01
  • 2013-01-14
  • 2013-07-29
  • 2013-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多