在 android webview 中从 blob 中获取数据

问题描述 投票:0回答:1

我正在创建一个应用程序,它使用

web view
加载网站并允许下载 IMG/PDF/GIF 等数据。问题是下载链接不是普通链接,它是
blob:
.

我知道

blob:
URL
不是指服务器上存在的数据,它指的是您的浏览器当前在内存中的数据。

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);
    }
}

我想下载设备存储中的文件。

有没有办法从

blob:
URL 获取数据?

是否可以将该数据发送到 chrome 进行下载。

我知道那里有很多问题,比如 StackOverflow 上的thisthis,我几乎都试过了,但没有发现任何这些问题。

java android download blob
1个回答
0
投票

更新后的

convertToGifAndProcess
方法将blob数据作为输入流读取并将其写入文件输出流,而无需将其转换为base64格式。

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());
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.