Rahul Khurana
Rahul Khurana

Reputation: 8844

Unable to get image receiving as blob

I am trying to read blob which contains image. I am able to read PDF from the below code:

web.setDownloadListener(new DownloadListener() {
        public void onDownloadStart(String url, String userAgent,
                                    String contentDisposition, String mimetype,
                                    long contentLength) {


            CommonUtilities._Log(TAG, "url = " + url + " ,, userAgent = " + userAgent + " ,,contentDisposition " + contentDisposition + " ,, mimetype = " + mimetype + " ,, length= " + contentLength);


            String s = "var request = new XMLHttpRequest();\n" +
                    "request.open(\'GET\', \"" + url + "\", true);\n" +
                    "request.responseType = \'blob\';\n" +
                    "request.timeout = 30000;" +
                    "request.onload = function() {\n" +
                    " var reader = new FileReader();\n" +
                    " reader.readAsDataURL(request.response); \n" +
                    " reader.onloadend = function() {\n" +
                    " base64data = reader.result; \n" +
                    " // console.log(base64data);\n" +
                    " Android.androidPay(base64data)" +
                    " }\n" +
                    "\n" +
                    "};\n" +
                    "request.send();";


            WebLink.this.web.evaluateJavascript(s, new ValueCallback<String>() {
                @Override
                public void onReceiveValue(String value) {
                    CommonUtilities._Log(TAG, "value " + value);
                }
            });
        }
    });

Here inside onDownloadStart method i am able to receive a blob. Then i am injecting Javascript code to fetch blob data. Android.androidPay(base64data) which receives string data.

private void writeToSDFile(String string, String fileName) {
    // Find the root of the external storage.
    // See http://developer.android.com/guide/topics/data/data-  storage.html#filesExternal
    File root = android.os.Environment.getExternalStorageDirectory();

    File dir = new File(root.getAbsolutePath() + "/" + Environment.DIRECTORY_DOWNLOADS);
    dir.mkdirs();
    File file = new File(dir, fileName);
    try {
        byte[] data = android.util.Base64.decode(string, Base64.DEFAULT);
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
        bos.write(data);
        bos.flush();
        bos.close();
        CommonUtilities._Log(TAG, "File Path " + file.getCanonicalPath());

        CommonUtilities.showFolderIntent(this, dir);
    } catch (Exception e) {
        CommonUtilities._Log(TAG, Log.getStackTraceString(e));
    }
}

The above method i am using to write data into a new file. I have tried multiple solutions for this problem but not able to resolve the error. With this method i am only able to recieve PDF file but no Image.

Upvotes: 1

Views: 739

Answers (1)

Sahil Manchanda
Sahil Manchanda

Reputation: 10012

Use the following Javascript string to get your Image/PDF from the blob object. Javascript ref. code has been taken from this

  String s ="var xhr = new XMLHttpRequest();\n"+
                    "xhr.open(\'GET\', \"" + url + "\", true);\n"+
                    "\n"+
                    "xhr.responseType = \'arraybuffer\';\n"+
                    "\n"+
                    "xhr.onload = function(e) {\n"+
                    " if (this.status == 200) {\n"+
                    " var uInt8Array = new Uint8Array(this.response);\n"+
                    " var i = uInt8Array.length;\n"+
                    " var binaryString = new Array(i);\n"+
                    " while (i--)\n"+
                    " {\n"+
                    " binaryString[i] = String.fromCharCode(uInt8Array[i]);\n"+
                    " }\n"+
                    " var data = binaryString.join(\'\');\n"+
                    "\n"+
                    " var base64 = window.btoa(data);\n"+
                    "\n"+
                    " Android.androidPay(base64)" +
                    " }\n"+
                    "};\n"+
                    "\n"+
                    "xhr.send();";

Upvotes: 3

Related Questions