下载从web视图文件到自定义文件夹 [英] Download a file from webview to a custom folder

查看:130
本文介绍了下载从web视图文件到自定义文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能从机器人的WebView下载文件到用户定义的目录。 目前该文件正在下载的SD卡/下载。是否有可能取代默认下载位置?

我目前使用下面的code下载文件

 意向意图=新的意图(Intent.ACTION_VIEW Uri.parse(下载文件位置));
startActivity(意向);
 

解决方案

当你的意思是你所需要的浏览器...你的意思是你需要处理的下载与浏览器?因为你可以使用web视图,仍然处理下载你自己。

由于@Harry喜悦评论,我会使用 shouldOverrideUrlLoading(web视图查看,字符串URL)方法和筛选这些网址要单独下载/扩展。如果你没有任何特定的文件扩展名或URL,你可能需要下载,但你可以编辑你的HTML / JavaScript的code,也许你可以做一些JavaScript技巧添加一个标志,让您的WebView识别URL作为下载。

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

 如果(sUserAgent == NULL){
        Log.e(TAG + - 体conexion的getString(R.string.e_envio_datos));
    }
    //创建客户端,并设置了特定的用户代理字符串
    HttpClient的客户端=新DefaultHttpClient();
    HttpPost请求=新HttpPost(URL);
    request.setHeader(用户代理,sUserAgent);
    尝试 {
        HTT presponse响应= client.execute(要求);
        //检查服务器的响应是否有效
        状态行状态= response.getStatusLine();
        如果(status.getStatus code()!= HTTP_STATUS_OK){
            // 错误
        } 其他 {
            。InputStream的时间= response.getEntity()的getContent();
            byte []的读取=新的字节[1024];
            INT numReadBytes = 0,singleByte;
            布尔endFlow = FALSE;
            做 {
                singleByte = in.read();
                endFlow = singleByte == -1;
                如果(!endFlow){
                    阅读[numReadBytes] =(字节)singleByte;
                    numReadBytes ++;
                }
            }而(endFlow!);
            如果(numReadBytes大于0){
    //在这里实现一些code到字节数组存储为文件
                storeDataWherever(读);
            }
        }
    }赶上(IOException异常E){
        Log.e(TAG + - 体conexion,e.getMessage());
    }赶上(ArrayIndexOutOfBoundsException异常E){
        Log.e(TAG + - 体conexion的getString(R.string.e_respuesta_size));
    }
 

Is it possible to download a file to a user defined directory from android webview. Currently the file is being downloaded in SDCard/downloads. Is it possible to override the default download location?

I am currently using the following code to download a file

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

解决方案

When you mean you need the browser... do you mean you need to handle the download with the browser? Cause you can use the webview and still handle the download yourself.

As @Harry Joy commented, I would use the shouldOverrideUrlLoading(WebView view, String url) method and filter those urls/extensions you want to download separately. If you don't have any specific file extensions or urls you may want to download, but you can edit your html/javascript code maybe you can do some javascript trick to add a flag and make your WebView recognize the url as a download.

To handle the download, maybe you already know, but it would be something like this

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

这篇关于下载从web视图文件到自定义文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆