在cordova插件中的字节分配内存不足

时间:2015-09-28 17:10:21

标签: android cordova

我编写了一个插件,它将来自适配器的base64字符串转换为字节数组并在本地系统上编写文件。 我正在使用pdf文件的base64,工作正常2 mb,但是当试图获得3mb的pdf文件的base64时,我有写问题。

我在log cat中遇到以下错误:

09-28 21:21:02.901: D/dalvikvm(20819): GC_CONCURRENT freed 10928K, 25% free 135145K/177984K, paused 47ms+38ms, total 174ms
09-28 21:21:04.173: D/dalvikvm(20819): GC_FOR_ALLOC freed 20551K, 28% free 114593K/157432K, paused 54ms, total 54ms
09-28 21:21:04.203: I/dalvikvm-heap(20819): Grow heap (frag case) to 131.301MB for 20310182-byte allocation
09-28 21:21:04.363: D/dalvikvm(20819): GC_FOR_ALLOC freed 0K, 25% free 134428K/177268K, paused 159ms, total 159ms
09-28 21:21:04.413: D/dalvikvm(20819): GC_CONCURRENT freed 410K, 24% free 134941K/177268K, paused 3ms+15ms, total 58ms
09-28 21:21:04.553: D/dalvikvm(20819): GC_FOR_ALLOC freed 4263K, 23% free 136765K/177268K, paused 37ms, total 37ms
09-28 21:21:04.673: D/dalvikvm(20819): GC_FOR_ALLOC freed 2336K, 23% free 137933K/177268K, paused 56ms, total 56ms
09-28 21:21:04.793: D/dalvikvm(20819): GC_FOR_ALLOC freed 3505K, 22% free 139686K/177268K, paused 45ms, total 45ms
09-28 21:21:04.804: I/dalvikvm-heap(20819): Grow heap (frag case) to 144.138MB for 8076058-byte allocation
09-28 21:21:04.864: D/dalvikvm(20819): GC_FOR_ALLOC freed 0K, 21% free 147573K/185156K, paused 59ms, total 59ms
09-28 21:21:05.014: D/dalvikvm(20819): GC_FOR_ALLOC freed 5257K, 24% free 142315K/185156K, paused 46ms, total 46ms
09-28 21:21:05.014: I/dalvikvm-heap(20819): Forcing collection of SoftReferences for 12114082-byte allocation
09-28 21:21:05.084: D/dalvikvm(20819): GC_BEFORE_OOM freed 66K, 24% free 142248K/185156K, paused 73ms, total 73ms
09-28 21:21:05.084: E/dalvikvm-heap(20819): Out of memory on a 12114082-byte allocation.
09-28 21:21:05.084: I/dalvikvm(20819): "WebViewCoreThread" prio=5 tid=14 RUNNABLE
09-28 21:21:05.084: I/dalvikvm(20819):   | group="main" sCount=0 dsCount=0 obj=0x427f7688 self=0x40633b08
09-28 21:21:05.084: I/dalvikvm(20819):   | sysTid=20843 nice=0 sched=0/0 cgrp=apps handle=1891741488
09-28 21:21:05.084: I/dalvikvm(20819):   | state=R schedstat=( 0 0 0 ) utm=2844 stm=356 core=1
09-28 21:21:05.084: I/dalvikvm(20819):   at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:~94)
09-28 21:21:05.084: I/dalvikvm(20819):   at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:162)
09-28 21:21:05.084: I/dalvikvm(20819):   at java.lang.StringBuilder.append(StringBuilder.java:311)
09-28 21:21:05.084: I/dalvikvm(20819):   at org.json.JSONTokener.nextString(JSONTokener.java:224)
09-28 21:21:05.084: I/dalvikvm(20819):   at org.json.JSONTokener.nextValue(JSONTokener.java:107)
09-28 21:21:05.084: I/dalvikvm(20819):   at org.json.JSONTokener.readArray(JSONTokener.java:430)
09-28 21:21:05.084: I/dalvikvm(20819):   at org.json.JSONTokener.nextValue(JSONTokener.java:103)
09-28 21:21:05.084: I/dalvikvm(20819):   at org.json.JSONArray.<init>(JSONArray.java:87)
09-28 21:21:05.084: I/dalvikvm(20819):   at org.json.JSONArray.<init>(JSONArray.java:103)
09-28 21:21:05.084: I/dalvikvm(20819):   at org.apache.cordova.CordovaPlugin.execute(CordovaPlugin.java:83)
09-28 21:21:05.084: I/dalvikvm(20819):   at org.apache.cordova.PluginManager.exec(PluginManager.java:151)
09-28 21:21:05.084: I/dalvikvm(20819):   at org.apache.cordova.CordovaBridge.jsExec(CordovaBridge.java:59)
09-28 21:21:05.084: I/dalvikvm(20819):   at org.apache.cordova.ExposedJsApi.exec(ExposedJsApi.java:40)
09-28 21:21:05.084: I/dalvikvm(20819):   at android.webkit.JWebCoreJavaBridge.setNetworkOnLine(Native Method)
09-28 21:21:05.084: I/dalvikvm(20819):   at android.webkit.JWebCoreJavaBridge.setNetworkOnLine(Native Method)
09-28 21:21:05.084: I/dalvikvm(20819):   at android.webkit.WebViewCore$EventHub$1.handleMessage(WebViewCore.java:1428)
09-28 21:21:05.084: I/dalvikvm(20819):   at android.os.Handler.dispatchMessage(Handler.java:99)
09-28 21:21:05.084: I/dalvikvm(20819):   at android.os.Looper.loop(Looper.java:137)
09-28 21:21:05.084: I/dalvikvm(20819):   at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:812)
09-28 21:21:05.084: I/dalvikvm(20819):   at java.lang.Thread.run(Thread.java:856)
09-28 21:21:05.084: W/System.err(20819): java.lang.OutOfMemoryError
09-28 21:21:05.084: W/System.err(20819):    at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:94)
09-28 21:21:05.084: W/System.err(20819):    at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:162)
09-28 21:21:05.084: W/System.err(20819):    at java.lang.StringBuilder.append(StringBuilder.java:311)
09-28 21:21:05.084: W/System.err(20819):    at org.json.JSONTokener.nextString(JSONTokener.java:224)
09-28 21:21:05.084: W/System.err(20819):    at org.json.JSONTokener.nextValue(JSONTokener.java:107)
09-28 21:21:05.084: W/System.err(20819):    at org.json.JSONTokener.readArray(JSONTokener.java:430)
09-28 21:21:05.094: W/System.err(20819):    at org.json.JSONTokener.nextValue(JSONTokener.java:103)
09-28 21:21:05.094: W/System.err(20819):    at org.json.JSONArray.<init>(JSONArray.java:87)
09-28 21:21:05.094: W/System.err(20819):    at org.json.JSONArray.<init>(JSONArray.java:103)
09-28 21:21:05.094: W/System.err(20819):    at org.apache.cordova.CordovaPlugin.execute(CordovaPlugin.java:83)
09-28 21:21:05.094: W/System.err(20819):    at org.apache.cordova.PluginManager.exec(PluginManager.java:151)
09-28 21:21:05.094: W/System.err(20819):    at org.apache.cordova.CordovaBridge.jsExec(CordovaBridge.java:59)
09-28 21:21:05.094: W/System.err(20819):    at org.apache.cordova.ExposedJsApi.exec(ExposedJsApi.java:40)
09-28 21:21:05.094: W/System.err(20819):    at android.webkit.JWebCoreJavaBridge.setNetworkOnLine(Native Method)
09-28 21:21:05.094: W/System.err(20819):    at android.webkit.JWebCoreJavaBridge.setNetworkOnLine(Native Method)
09-28 21:21:05.094: W/System.err(20819):    at android.webkit.WebViewCore$EventHub$1.handleMessage(WebViewCore.java:1428)
09-28 21:21:05.094: W/System.err(20819):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-28 21:21:05.094: W/System.err(20819):    at android.os.Looper.loop(Looper.java:137)
09-28 21:21:05.094: W/System.err(20819):    at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:812)
09-28 21:21:05.094: W/System.err(20819):    at java.lang.Thread.run(Thread.java:856)
09-28 21:21:05.104: I/Web Console(20819): No matching configurations found from the server. Defaulting to local configuration at file:///android_asset/www/default/worklight/worklight.js:5780
09-28 21:21:05.434: I/Web Console(20819): "e22e7e76a1b4028eae226686f9120ac6" at file:///android_asset/www/default/js/main.js:244
09-28 21:21:05.965: D/dalvikvm(20819): GC_FOR_ALLOC freed 27742K, 28% free 114529K/157432K, paused 48ms, total 48ms
09-28 21:21:05.975: I/dalvikvm-heap(20819): Grow heap (frag case) to 121.141MB for 9663722-byte allocation
09-28 21:21:06.065: D/dalvikvm(20819): GC_FOR_ALLOC freed 0K, 26% free 123966K/166872K, paused 91ms, total 91ms
09-28 21:21:06.155: D/dalvikvm(20819): GC_CONCURRENT freed 1555K, 25% free 125911K/166872K, paused 10ms+22ms, total 85ms
09-28 21:21:06.285: D/dalvikvm(20819): GC_FOR_ALLOC freed 3695K, 25% free 126592K/166872K, paused 65ms, total 65ms
09-28 21:21:06.405: D/dalvikvm(20819): GC_FOR_ALLOC freed 2625K, 24% free 127904K/166872K, paused 53ms, total 53ms
09-28 21:21:06.555: D/dalvikvm(20819): GC_FOR_ALLOC freed 3938K, 23% free 129873K/166872K, paused 42ms, total 42ms
09-28 21:21:06.565: I/dalvikvm-heap(20819): Grow heap (frag case) to 135.563MB for 9072964-byte allocation
09-28 21:21:06.635: D/dalvikvm(20819): GC_FOR_ALLOC freed 0K, 22% free 138734K/175736K, paused 75ms, total 75ms
09-28 21:21:06.716: D/dalvikvm(20819): GC_CONCURRENT freed 5906K, 25% free 132827K/175736K, paused 4ms+16ms, total 76ms
09-28 21:21:06.806: D/dalvikvm(20819): GC_FOR_ALLOC freed <1K, 25% free 132827K/175736K, paused 35ms, total 35ms
09-28 21:21:06.826: I/dalvikvm-heap(20819): Grow heap (frag case) to 142.773MB for 13609442-byte allocation
09-28 21:21:06.896: D/dalvikvm(20819): GC_FOR_ALLOC freed 8860K, 28% free 137257K/189028K, paused 69ms, total 69ms
09-28 21:21:06.976: D/dalvikvm(20819): GC_CONCURRENT freed <1K, 28% free 137257K/189028K, paused 17ms+40ms, total 88ms
09-28 21:21:06.976: D/dalvikvm(20819): WAIT_FOR_CONCURRENT_GC blocked 67ms
09-28 21:21:06.976: I/dalvikvm-heap(20819): Forcing collection of SoftReferences for 9422036-byte allocation
09-28 21:21:07.086: D/dalvikvm(20819): GC_BEFORE_OOM freed <1K, 28% free 137256K/189028K, paused 119ms, total 119ms
09-28 21:21:07.086: E/dalvikvm-heap(20819): Out of memory on a 9422036-byte allocation.
09-28 21:21:07.086: I/dalvikvm(20819): "WebViewCoreThread" prio=5 tid=14 RUNNABLE
09-28 21:21:07.086: I/dalvikvm(20819):   | group="main" sCount=0 dsCount=0 obj=0x427f7688 self=0x40633b08
09-28 21:21:07.086: I/dalvikvm(20819):   | sysTid=20843 nice=0 sched=0/0 cgrp=apps handle=1891741488
09-28 21:21:07.086: I/dalvikvm(20819):   | state=R schedstat=( 0 0 0 ) utm=2982 stm=367 core=1
09-28 21:21:07.086: I/dalvikvm(20819):   at java.lang.String.<init>(String.java:~422)
09-28 21:21:07.086: I/dalvikvm(20819):   at java.lang.AbstractStringBuilder.toString(AbstractStringBuilder.java:642)
09-28 21:21:07.086: I/dalvikvm(20819):   at java.lang.StringBuilder.toString(StringBuilder.java:663)
09-28 21:21:07.086: I/dalvikvm(20819):   at org.json.JSONTokener.nextString(JSONTokener.java:213)
09-28 21:21:07.086: I/dalvikvm(20819):   at org.json.JSONTokener.nextValue(JSONTokener.java:107)
09-28 21:21:07.086: I/dalvikvm(20819):   at org.json.JSONTokener.readArray(JSONTokener.java:430)
09-28 21:21:07.086: I/dalvikvm(20819):   at org.json.JSONTokener.nextValue(JSONTokener.java:103)
09-28 21:21:07.086: I/dalvikvm(20819):   at org.json.JSONArray.<init>(JSONArray.java:87)
09-28 21:21:07.086: I/dalvikvm(20819):   at org.json.JSONArray.<init>(JSONArray.java:103)
09-28 21:21:07.086: I/dalvikvm(20819):   at org.apache.cordova.CordovaPlugin.execute(CordovaPlugin.java:83)
09-28 21:21:07.096: I/dalvikvm(20819):   at org.apache.cordova.PluginManager.exec(PluginManager.java:151)
09-28 21:21:07.096: I/dalvikvm(20819):   at org.apache.cordova.CordovaBridge.jsExec(CordovaBridge.java:59)
09-28 21:21:07.096: I/dalvikvm(20819):   at org.apache.cordova.ExposedJsApi.exec(ExposedJsApi.java:40)
09-28 21:21:07.096: I/dalvikvm(20819):   at android.webkit.JWebCoreJavaBridge.setNetworkOnLine(Native Method)
09-28 21:21:07.096: I/dalvikvm(20819):   at android.webkit.JWebCoreJavaBridge.setNetworkOnLine(Native Method)
09-28 21:21:07.096: I/dalvikvm(20819):   at android.webkit.WebViewCore$EventHub$1.handleMessage(WebViewCore.java:1428)
09-28 21:21:07.096: I/dalvikvm(20819):   at android.os.Handler.dispatchMessage(Handler.java:99)
09-28 21:21:07.096: I/dalvikvm(20819):   at android.os.Looper.loop(Looper.java:137)
09-28 21:21:07.096: I/dalvikvm(20819):   at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:812)
09-28 21:21:07.096: I/dalvikvm(20819):   at java.lang.Thread.run(Thread.java:856)
09-28 21:21:07.096: W/System.err(20819): java.lang.OutOfMemoryError
09-28 21:21:07.096: W/System.err(20819):    at java.lang.String.<init>(String.java:422)
09-28 21:21:07.096: W/System.err(20819):    at java.lang.AbstractStringBuilder.toString(AbstractStringBuilder.java:642)
09-28 21:21:07.096: W/System.err(20819):    at java.lang.StringBuilder.toString(StringBuilder.java:663)
09-28 21:21:07.096: W/System.err(20819):    at org.json.JSONTokener.nextString(JSONTokener.java:213)
09-28 21:21:07.096: W/System.err(20819):    at org.json.JSONTokener.nextValue(JSONTokener.java:107)
09-28 21:21:07.096: W/System.err(20819):    at org.json.JSONTokener.readArray(JSONTokener.java:430)
09-28 21:21:07.096: W/System.err(20819):    at org.json.JSONTokener.nextValue(JSONTokener.java:103)
09-28 21:21:07.096: W/System.err(20819):    at org.json.JSONArray.<init>(JSONArray.java:87)
09-28 21:21:07.096: W/System.err(20819):    at org.json.JSONArray.<init>(JSONArray.java:103)
09-28 21:21:07.096: W/System.err(20819):    at org.apache.cordova.CordovaPlugin.execute(CordovaPlugin.java:83)
09-28 21:21:07.096: W/System.err(20819):    at org.apache.cordova.PluginManager.exec(PluginManager.java:151)
09-28 21:21:07.096: W/System.err(20819):    at org.apache.cordova.CordovaBridge.jsExec(CordovaBridge.java:59)
09-28 21:21:07.096: W/System.err(20819):    at org.apache.cordova.ExposedJsApi.exec(ExposedJsApi.java:40)
09-28 21:21:07.096: W/System.err(20819):    at android.webkit.JWebCoreJavaBridge.setNetworkOnLine(Native Method)
09-28 21:21:07.096: W/System.err(20819):    at android.webkit.JWebCoreJavaBridge.setNetworkOnLine(Native Method)
09-28 21:21:07.096: W/System.err(20819):    at android.webkit.WebViewCore$EventHub$1.handleMessage(WebViewCore.java:1428)
09-28 21:21:07.096: W/System.err(20819):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-28 21:21:07.096: W/System.err(20819):    at android.os.Looper.loop(Looper.java:137)
09-28 21:21:07.096: W/System.err(20819):    at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:812)
09-28 21:21:07.096: W/System.err(20819):    at java.lang.Thread.run(Thread.java:856)

我写的插件如下:

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
import android.provider.MediaStore.MediaColumns;
import android.util.Base64;

import java.security.MessageDigest;
import java.io.File.*;
import java.io.*;
import java.net.URL;

public class Base64Plugin extends CordovaPlugin{
int REQUEST_CAMERA = 0, SELECT_FILE = 1;
CallbackContext callbackContext;

public boolean execute(String action, JSONArray args,
        CallbackContext callbackContext) throws JSONException {

    System.out.println(action);
    System.out.println("param:::"+args.getString(0));
    System.out.println("param:::"+args.getString(1));
    if (action.equals("download")) {

        this.callbackContext = callbackContext;
        FileOutputStream fileOutputStream = null;
        int buffer = 1024;
        int SIZE = args.getString(2).length();
        int start = 0;
        int end = buffer;

        JSONObject res = new JSONObject();
        //byte[] data = new byte[buffer];//Base64.decode(args.getString(0), Base64.DEFAULT);
        File file = new File("/storage/emulated/0/Pictures/Screenshots/", args.getString(0));
        try {
            fileOutputStream = new FileOutputStream(file);
            while(start < SIZE) { 
                System.out.println("start:"+start+" \n end:"+end);
                byte[] data = Base64.decode(args.getString(2).substring(start, end), Base64.DEFAULT);
                fileOutputStream.write(data);
                start = end;
                if(start + buffer<= SIZE){
                    end = start + buffer;
                }else{
                    end = SIZE;
                }
                data = null;
            }

            res.put("Result", "File Write Successfule");

            this.callbackContext.success(res);
            //data = null;
            fileOutputStream = null;
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            res.put("Error", e.getMessage());
            this.callbackContext.error(res);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            res.put("Error", e.getMessage());
            this.callbackContext.error(res);
        }

    }else if(action.equals("upload")){
        this.callbackContext = callbackContext;
        String encodedImage = null;
        String selectedImagePath = "";
        StringBuffer sb = new StringBuffer("");
        try {

            System.out.println("creating variables..");
            FileInputStream fileInputStream = null;

            File file = new File("/storage/emulated/0/Pictures/Screenshots/"+args.getString(0));
            byte[] bFile = new byte[(int) file.length()];

            fileInputStream = new FileInputStream(file);
            BufferedInputStream inputStream = new BufferedInputStream(fileInputStream);
            System.out.println("reading file...");
            inputStream.read(bFile);

            MessageDigest md1 = MessageDigest.getInstance("MD5");

            int nread = 0;
            System.out.println("reading bytes....");
            while ((nread = fileInputStream.read(bFile)) != -1) {
                md1.update(bFile, 0, nread);
            };

            byte[] mdbytes = md1.digest(bFile);

            // convert the byte to hex format
            System.out.println("generating checksum....");
            for (int i = 0; i < mdbytes.length; i++) {
                sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100,
                        16).substring(1));
            }

            System.out.println("generating base64....");
            encodedImage = Base64.encodeToString(bFile, Base64.DEFAULT);

        } catch (Exception e) {

        }
        // System.out.println("base64:"+encodedImage);

        JSONObject res = new JSONObject();
        try {
            System.out.println("adding checksum and base64 to result....");
            res.put("Result", encodedImage);
            res.put("Checksum", sb);

            res.put("path", args.getString(0));
            System.out.println("sending result....");
            this.callbackContext.success(res);

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            encodedImage = null;
            //bFile = null;
        }

        return true;

    }
    return false;
}

}

0 个答案:

没有答案