gpt4 book ai didi

Android webview 下载数据/文本文件

转载 作者:行者123 更新时间:2023-12-05 00:08:40 31 4
gpt4 key购买 nike

我开发了一个 android webview 并尝试在单击 webview 中的链接时下载生成的数据:文本文件。

    webView.setDownloadListener(new DownloadListener() {
@Override
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimeType, long contentLength) {
DownloadManager.Request request = new DownloadManager.Request(
Uri.parse(url));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "download");
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
}
});

但是,我收到以下错误异常:
java.lang.IllegalArgumentException: Can only download HTTP/HTTPS URIs: data:text/vcard;base64,qadsasffaefqawdfafafasdasdasdasdadasd

有什么解决办法吗?提前致谢。

最佳答案

mWebView.setDownloadListener(new DownloadListener() {       

public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
DownloadManager.Request request = new DownloadManager.Request(
Uri.parse(url));

request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //Notify client once download is completed!
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "Name of your downloadble file goes here, example: Mathematics II ");
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); //This is important!
intent.addCategory(Intent.CATEGORY_OPENABLE); //CATEGORY.OPENABLE
intent.setType("*/*");//any application,any extension
Toast.makeText(getApplicationContext(), "Downloading File", //To notify the Client that the file is being downloaded
Toast.LENGTH_LONG).show();

}
});

不要忘记授予此权限!这个非常重要!将此添加到您的 list 文件中(AndroidManifest.xml 文件)
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
希望这可以帮助。干杯:)

关于Android webview 下载数据/文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37313496/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com