抛出OutMfory错误(字节到Base64)

时间:2015-11-17 17:00:42

标签: android android-studio bitmap

我有一个数据库,我有一些大图像可以将它转换为base64 当我将它保存到数据库时,我收到了这个错误。

11-17 11:56:03.359 11549-11620/? E/art: Throwing OutOfMemoryError "Failed to allocate a 63489036 byte allocation with 16777216 free bytes and 47MB until OOM"
11-17 11:56:03.359 11549-11620/? E/AndroidRuntime: FATAL EXCEPTION: Thread-792
11-17 11:56:03.359 11549-11620/? E/AndroidRuntime: Process: com.juandirection, PID: 11549
11-17 11:56:03.359 11549-11620/? E/AndroidRuntime: java.lang.OutOfMemoryError: Failed to allocate a 63489036 byte allocation with 16777216 free bytes and 47MB until OOM
11-17 11:56:03.359 11549-11620/? E/AndroidRuntime:     at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
11-17 11:56:03.359 11549-11620/? E/AndroidRuntime:     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
11-17 11:56:03.359 11549-11620/? E/AndroidRuntime:     at android.graphics.BitmapFactory.decodeStreamInternal(BitmapFactory.java:635)
11-17 11:56:03.359 11549-11620/? E/AndroidRuntime:     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:611)
11-17 11:56:03.359 11549-11620/? E/AndroidRuntime:     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:649)
11-17 11:56:03.359 11549-11620/? E/AndroidRuntime:     at com.juandirection.Review$9.getParams(Review.java:206)
11-17 11:56:03.359 11549-11620/? E/AndroidRuntime:     at com.android.volley.Request.getBody(Request.java:468)
11-17 11:56:03.359 11549-11620/? E/AndroidRuntime:     at com.android.volley.toolbox.HurlStack.addBodyIfExists(HurlStack.java:253)
11-17 11:56:03.359 11549-11620/? E/AndroidRuntime:     at com.android.volley.toolbox.HurlStack.setConnectionParametersForRequest(HurlStack.java:227)
11-17 11:56:03.359 11549-11620/? E/AndroidRuntime:     at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:107)
11-17 11:56:03.359 11549-11620/? E/AndroidRuntime:     at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:97)
11-17 11:56:03.359 11549-11620/? E/AndroidRuntime:     at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:114)

我的图片大约是6MB 有些只是KB。 Kb图像工作正常但是当我上传6mb图像大小时,我得到了这个错误。

  @Override
                protected Map<String, String> getParams() {
                    Map<String, String> parameters = new HashMap<String, String>();
                    //String filepath = "/storage/emulated/0/Pictures/20150426_085939.jpg";
                    String filepath = Global.SELECTED_IMAGES.get(valueThisIteration);
                    String newFilePath = filepath.substring(7,filepath.length());
                    File imagefile = new File(newFilePath);
                    FileInputStream fis = null;
                    try {
                        fis = new FileInputStream(imagefile);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }

                    Bitmap bm = BitmapFactory.decodeStream(fis);
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    bm.compress(Bitmap.CompressFormat.JPEG, 70 , baos);
                    byte[] b = baos.toByteArray();
                    String encImage = Base64.encodeToString(b, Base64.DEFAULT);
                    parameters.put("latitude", Global.lat);
                    parameters.put("imagepath", encImage);
                    return parameters;
                }

3 个答案:

答案 0 :(得分:1)

您可以尝试在android:largeHeap="true"文件的应用标记中设置AndroidManifest.xml。如下所示:

<application
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:largeHeap="true" />

希望这有帮助。

答案 1 :(得分:0)

不要在哈希映射或数组列表中存储任何位图或base64字符串只存储图像的路径,当你想使用图像时,从该路径获取位图或base64图像字符串,这样做是很好的做法

  Map<String, String> parameters = new HashMap<String, String>();

此地图仅存储图像的地址路径

答案 2 :(得分:0)

您必须更改图像的分辨率。 Android根本无法存储大量图片,因为它会占用太多内存。