2

I'm creating an application that uses web view to load the website and allow to download data like IMG/PDF/GIF. The problem is that the download link is not a normal link it's blob:.

I know blob: URL does not refer to the data that exists on the server, it refers to data that your browser currently has in memory.

class DownloadBlobFileJSInterface {

    private Context mContext;
    private DownloadGifSuccessListener mDownloadGifSuccessListener;

    public DownloadBlobFileJSInterface(Context context) {
        this.mContext = context;
    }

    public static String getBase64StringFromBlobUrl(String blobUrl) {
        if (blobUrl.startsWith("blob")) {
            return "javascript: var xhr = new XMLHttpRequest();" +
                    "xhr.open('GET', '" + blobUrl + "', true);" +
                    "xhr.setRequestHeader('Content-type','image/gif');" +
                    "xhr.responseType = 'blob';" +
                    "xhr.onload = function(e) {" +
                    "    if (this.status == 200) {" +
                    "        var blobFile = this.response;" +
                    "        var reader = new FileReader();" +
                    "        reader.readAsDataURL(blobFile);" +
                    "        reader.onloadend = function() {" +
                    "            base64data = reader.result;" +
                    "            Android.getBase64FromBlobData(base64data);" +
                    "        }" +
                    "    }" +
                    "};" +
                    "xhr.send();";
        }
        return "javascript: console.log('It is not a Blob URL');";
    }

    public void setDownloadGifSuccessListener(DownloadGifSuccessListener listener) {
        mDownloadGifSuccessListener = listener;
    }

    @JavascriptInterface
    public void getBase64FromBlobData(String base64Data) {
        convertToGifAndProcess(base64Data);
    }

    private void convertToGifAndProcess(String base64) {

        ContextWrapper wrapper = new ContextWrapper(mContext);
        String fullPath =wrapper.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).toString();

        File gifFile = new File(fullPath+ "/File_" + System.currentTimeMillis() + "_.gif");


        saveGifToPath(base64, gifFile);
        Toast.makeText(mContext, "Downloaded", Toast.LENGTH_SHORT).show();
        if (mDownloadGifSuccessListener != null) {
            mDownloadGifSuccessListener.downloadGifSuccess(gifFile.getAbsolutePath());
        }
    }

    private void saveGifToPath(String base64, File gifFilePath) {
        try {
            byte[] fileBytes = Base64.decode(base64.replaceFirst(
                    "data:image/gif;base64,", ""), 0);
            FileOutputStream os = new FileOutputStream(gifFilePath, false);
            os.write(fileBytes);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public interface DownloadGifSuccessListener {
        void downloadGifSuccess(String absolutePath);
    }
}

I want to download the file in device storage.

Is there any way to get data from the blob: URL?

Or

Is it possible to send that data to chrome to download.

I know there is a lot of questions out there like this, this on StackOverflow, I almost tried all but didn't find any of these working.

Marsad
  • 859
  • 1
  • 14
  • 35
  • @RomanSoviak I posted it as an answer, but haven't tested it yet. Let me know if this is working or not – Marsad Apr 11 '23 at 16:03

1 Answers1

0

The updated convertToGifAndProcess method reads the blob data as an input stream and writes it to the file output stream without converting it to base64 format.

private void convertToGifAndProcess(String base64) {
    ContextWrapper wrapper = new ContextWrapper(mContext);
    String fullPath = wrapper.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).toString();
    File gifFile = new File(fullPath+ "/File_" + System.currentTimeMillis() + "_.gif");

    try {
        URL url = new URL(base64);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        FileOutputStream output = new FileOutputStream(gifFile);
        byte[] buffer = new byte[1024];
        int bytesRead;
        while ((bytesRead = input.read(buffer)) != -1) {
            output.write(buffer, 0, bytesRead);
        }
        output.flush();
        output.close();
        input.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    Toast.makeText(mContext, "Downloaded ", Toast.LENGTH_SHORT).show();
    if (mDownloadGifSuccessListener != null) {
        mDownloadGifSuccessListener.downloadGifSuccess(gifFile.getAbsolutePath());
    }
}
Marsad
  • 859
  • 1
  • 14
  • 35