我正在创建一个应用程序,它使用
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);
}
}
我想下载设备存储中的文件。
有没有办法从
URL 获取数据?blob:
或
是否可以将该数据发送到 chrome 进行下载。
我知道那里有很多问题,比如 StackOverflow 上的this、this,我几乎都试过了,但没有发现任何这些问题。
更新后的
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());
}
}