2016-12-05 81 views
0

我已經在我的android應用程序中實現了一個webview,並通過該webview我調用javascript函數。從該函數一個函數接受基64字符串。base64字符串不接受從Android的JavaScript功能

但問題是,javascript函數接受底座64串其圖像大小小於3 MB.If圖像尺寸大於3 MB更大的那些圖像的那麼它不接受底座64串

從代碼Android應用是如下

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent intent) { 
    switch (requestCode) { 
     case 1: 
      if (resultCode == RESULT_OK) { 
       Uri selectedImage = intent.getData(); 
       String path = getRealPathFromURI(this, selectedImage); 
       File img = new File(path); 
       InputStream inputStream = null; 
       try { 
        inputStream = new FileInputStream(img.getAbsoluteFile()); 
        byte[] bytes; 
        byte[] buffer = new byte[8192]; 
        int bytesRead; 
        ByteArrayOutputStream output = new ByteArrayOutputStream(); 
        try { 
         while ((bytesRead = inputStream.read(buffer)) != -1) { 
          output.write(buffer, 0, bytesRead); 

         } 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } 
        bytes = output.toByteArray(); 
        String img_str = Base64.encodeToString(bytes, Base64.DEFAULT); 
        Log.v("base abhi111111111", "" + img_str); 

        editor.loadUrl("javascript:getFilePath('" + img_str + "')"); 
       } catch (FileNotFoundException e) { 
        e.printStackTrace(); 
       } 


      } 
      break; 
     case 2: 
      if (resultCode == RESULT_OK) { 
       Uri selectedImage = intent.getData(); 
       String path = getRealPathFromURI(this, selectedImage); 
       File img = new File(path); 
       InputStream inputStream = null; 
       try { 
        inputStream = new FileInputStream(img.getAbsoluteFile()); 
        byte[] bytes; 
        byte[] buffer = new byte[8192]; 
        int bytesRead; 
        ByteArrayOutputStream output = new ByteArrayOutputStream(); 
        try { 
         while ((bytesRead = inputStream.read(buffer)) != -1) { 
          output.write(buffer, 0, bytesRead); 
         } 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } 
        bytes = output.toByteArray(); 
        String img_str = Base64.encodeToString(bytes, Base64.DEFAULT); 

        Log.v("base abhi", "" + img_str); 
        editor.loadUrl("javascript:getFilePath1('" + img_str + "')"); 
       } catch (FileNotFoundException e) { 
        e.printStackTrace(); 
       } 
      } 
      break; 
    } 
} 

和在JavaScript的功能是作爲follows.that javascript函數接受底座64串的圖像尺寸小於MB的。

getfilepath(file) 
    { 
    var file1 = "data:image/jpeg;base64," + file; 
      console.write(file); 
      var imgObj = new Image(); 
      imgObj.src = file1; 
      imgObj.onload = function() { 
       var wt1 = this.width; 
       var ht1 = this.height; 

       if (wt1 < 150 || ht1 < 150) { 
        alert("low quality image!!!.."); 
       } 
       if (wt1 < 550 && ht1 < 400) { 
        canvas11.setWidth(wt1); 
        canvas11.setHeight(ht1); 
       } 
       if (wt1 > 550 && ht1 > 400) { 
        ht2 = 370; 
        wt2 = (wt1 * 370)/ht1; 
        canvas11.setWidth(wt2); 
        canvas11.setHeight(ht2); 
       } 
       if (wt1 < 550 && ht1 > 400) { 
        ht2 = 400; 
        wt2 = (wt1 * 400)/ht1; 
        canvas11.setWidth(wt2); 
        canvas11.setHeight(ht2); 
       } 
       if (wt1 > 550 && ht1 < 400) { 
        wt2 = 550; 
        ht2 = (ht1 * 550)/wt1; 
        canvas11.setWidth(wt2); 
        canvas11.setHeight(ht2); 
       } 
       document.getElementById('openerImg').click(); 

       canvas.backgroundColor = ''; 
       var height1 = canvas11.height; 
       var width1 = canvas11.width; 
       var image = new fabric.Image(imgObj); 
       image.set({ 
        width: width1, 
        height: height1 
       }); 
       canvas11.setBackgroundImage(image, canvas11.renderAll.bind(canvas11)); 

      }; 

     } 

請給solution.I didnt從去年2 months.Please得到解決

感謝很多提前

logcat的,如果我java.lang.OutOfMemoryError: Failed to allocate a 31082250 byte allocation with 4182256 free bytes and 19MB until OOM at java.lang.StringFactory.newStringFromChars(Native Method) at java.lang.AbstractStringBuilder.toString(AbstractStringBuilder.java:629) at java.lang.StringBuilder.toString(StringBuilder.java:663) at com.dhruva.eprintpost.home.Web_Editor.onActivityResult(Web_Editor.java:297) at android.app.Activity.dispatchActivityResult(Activity.java:6492) at android.app.ActivityThread.deliverResults(ActivityThread.java:3925) at android.app.ActivityThread.handleSendResult(ActivityThread.java:3972) at android.app.ActivityThread.-wrap16(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1537) at android.os.Handler.dispatchMessage(Handler.java:111) at android.os.Looper.loop(Looper.java:207) at android.app.ActivityThread.main(ActivityThread.java:5728) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679) 12

回答

0

壓縮您的位圖或圖像,試試這個代碼

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent intent) { 
    switch (requestCode) { 
     case 1: 
      if (resultCode == RESULT_OK) { 
       Uri selectedImage = intent.getData(); 
       String path = getRealPathFromURI(this, selectedImage); 
       File img = new File(path); 
       Bitmap bitmap= BitmapFactory.decodeFile(img) 
       ByteArrayOutputStream baos=new ByteArrayOutputStream(); 
       bitmap.compress(Bitmap.CompressFormat.PNG,100, baos); 
       byte [] bytes=baos.toByteArray(); 
     String img_str = Base64.encodeToString(b, Base64.DEFAULT); 
       Log.v("base abhi111111111", "" + img_str); 
        editor.loadUrl("javascript:getFilePath('" + img_str + "')"); 
       } catch (FileNotFoundException e) { 
        e.printStackTrace(); 
       } 


      } 
      break; 
     case 2: 
      if (resultCode == RESULT_OK) { 
       Uri selectedImage = intent.getData(); 
       String path = getRealPathFromURI(this, selectedImage); 
       File img = new File(path); 
       Bitmap bitmap= BitmapFactory.decodeFile(img) 
       ByteArrayOutputStream baos=new ByteArrayOutputStream(); 
       bitmap.compress(Bitmap.CompressFormat.PNG,100, baos); 
       byte [] bytes=baos.toByteArray(); 
     String img_str = Base64.encodeToString(b, Base64.DEFAULT); 
        Log.v("base abhi", "" + img_str); 
        editor.loadUrl("javascript:getFilePath1('" + img_str + "')"); 
       } catch (FileNotFoundException e) { 
        e.printStackTrace(); 
       } 
      } 
      break; 
    } 
} 

嘗試更改您的清單文件Ë線android:largeHeap="true"

<application 
    android:allowBackup="true" 
    android:hardwareAccelerated="false" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:largeHeap="true" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
+0

我想這其中也,但我得到OutOfMemoryException異常 – jones

+0

我不想損失圖像 – jones

+0

質量u能張貼logcat的..現在 –