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

Android 将文件从webview下载到自定义文件夹,android,download,Android,Download,是否可以从android webview将文件下载到用户定义的目录中。 当前文件正在SDCard/downloads中下载。是否可以覆盖默认下载位置 我目前正在使用以下代码下载一个文件 Intent intent = new Intent(Intent.ACTION_VIEW Uri.parse("download file location")); startActivity(intent); 当你的意思是你需要浏览器时。。。你是说你需要用浏览器处理下载吗?因为您可以使用webview,但

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

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

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

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

正如@Harry Joy所评论的,我将使用
shouldOverrideUrlLoading(WebView视图,字符串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));       
    }

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

正如@Harry Joy所评论的,我将使用
shouldOverrideUrlLoading(WebView视图,字符串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));       
    }

同样的问题:但没有答案。作为一个黑客/解决方案:不要使用浏览器下载。自己处理下载,你可以把它存储在任何你可以写的地方。如果您使用浏览器进行下载,它将在浏览器想要的地方下载,而不是在您想要的地方。@harry感谢buddy提供了您的解决方案。但不幸的是,我需要浏览器。同样的问题:但没有答案。作为黑客/解决方案:不要使用浏览器进行下载。自己处理下载,你可以把它存储在任何你可以写的地方。如果您使用浏览器进行下载,它将在浏览器想要的地方下载,而不是在您想要的地方。@harry感谢buddy的解决方案。但不幸的是,我需要浏览器。我想下载DRM内容,当我使用HTTPClient保存文件时,Android无法理解它是DRM文件,所以前向锁定正在发生。Android只有在通过浏览器下载时才能理解DRM内容,但在那里我无法将文件保存到所需的目录。有解决方案吗?谢谢。由于安全问题,我不确定带有本地url的Intent.ACTION\视图是否有效。但是,要将数据(字节)下载到自定义位置,您不需要Android来理解该类型的文件。如果用户必须通过WebView登录,那么在获取文件时,该代码会保持身份验证,还是只对不安全的文件有效?@libertas,我从来没有尝试过像您所说的,但是我认为它们是不同的会话,因此,使用这个示例代码,您不需要维护身份验证。尽管如此,我还是会尝试使用以前的表单获取用户凭据,并通过方法
setHttpAuthUsernamePassword
将其用于
WebView
和http请求对象。要为http请求设置凭据,有大量关于如何设置凭据的文档。好luck@mdelolmo我知道“setHttpAuthUsernamePassword”是如何用于WebView的,但是我将使用什么来验证HttpClient/Post/Response?如果我用它发送表单帖子,有没有办法维护cookie之类的东西?我想下载一个DRM内容,当我使用HTTPClient保存文件时,Android无法理解它是一个DRM文件,所以发生了前向锁定。Android只有在通过浏览器下载时才能理解DRM内容,但在那里,我无法将文件保存到所需的目录。有解决方案吗?谢谢。由于安全问题,我不确定带有本地url的Intent.ACTION\视图是否有效。但是,要将数据(字节)下载到自定义位置,您不需要Android来理解该类型的文件。如果用户必须通过WebView登录,那么在获取文件时,该代码会保持身份验证,还是只对不安全的文件有效?@libertas,我从来没有尝试过像您所说的,但是我认为它们是不同的会话,因此,使用这个示例代码,您不需要维护身份验证。尽管如此,我还是会尝试使用以前的表单获取用户凭据,并通过方法
setHttpAuthUsernamePassword
将其用于
WebView
和http请求对象。要为http请求设置凭据,有大量关于如何设置凭据的文档。好luck@mdelolmo我知道“setHttpAuthUsernamePassword”是如何用于WebView的,但是我将使用什么来验证HttpClient/Post/Response?如果我用它发送表单,有没有办法维护cookie之类的东西?