gpt4 book ai didi

android - 将文件从 webview 下载到自定义文件夹

转载 作者:行者123 更新时间:2023-11-30 04:46:28 25 4
gpt4 key购买 nike

是否可以从 android webview 下载文件到用户定义的目录。目前该文件正在 SDCard/downloads 中下载。是否可以覆盖默认下载位置?

我目前正在使用以下代码下载一个文件

Intent intent = new Intent(Intent.ACTION_VIEW Uri.parse("download file location"));
startActivity(intent);

最佳答案

当您的意思是您需要浏览器时...您的意思是您需要使用浏览器处理下载吗?因为您可以使用 webview 并仍然自己处理下载。

正如@Harry Joy 评论的那样,我会使用 shouldOverrideUrlLoading(WebView view, String url) 方法并过滤您要单独下载的那些 url/扩展。如果您没有任何特定的文件扩展名或 url,您可能想要下载,但您可以编辑您的 html/javascript 代码,也许您可​​以做一些 javascript 技巧来添加一个标志并让您的 WebView 将 url 识别为下载。

要处理下载,也许你已经知道了,但它会是这样的

    if (sUserAgent == null) {
Log.e(TAG + " - Conexion", getString(R.string.e_envio_datos));
}
// Create client and set our specific user-agent string
HttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost(url);
request.setHeader("User-Agent", sUserAgent);
try {
HttpResponse response = client.execute(request);
// Check if server response is valid
StatusLine status = response.getStatusLine();
if (status.getStatusCode() != HTTP_STATUS_OK) {
// Error
} else {
InputStream in = response.getEntity().getContent();
byte[] read = new byte [1024];
int numReadBytes= 0, singleByte;
boolean endFlow= false;
do {
singleByte= in.read();
endFlow = singleByte == -1;
if (!endFlow) {
read[numReadBytes] = (byte) singleByte;
numReadBytes++;
}
} while (!endFlow);
if (numReadBytes> 0) {
// Here you implement some code to store the array of bytes as a file
storeDataWherever(read);
}
}
} catch (IOException e) {
Log.e(TAG + " - Conexion", e.getMessage());
} catch (ArrayIndexOutOfBoundsException e){
Log.e(TAG + " - Conexion", getString(R.string.e_respuesta_size));
}

关于android - 将文件从 webview 下载到自定义文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4860484/

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