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.