[ 
https://issues.apache.org/jira/browse/CB-12834?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16116436#comment-16116436
 ] 

ASF GitHub Bot commented on CB-12834:
-------------------------------------

Github user RafaelKr commented on the issue:

    https://github.com/apache/cordova-plugin-inappbrowser/pull/221
  
    This is awesome!
    But can you please change the function `processDownload` like this?
    
    ```Java
    import android.webkit.CookieManager;
    
    [...]
    
    protected void processDownload() {
        final String url = InAppBrowserDownloads.this.url;
        final String cookie = CookieManager.getInstance().getCookie(url);
        final String filename = URLUtil.guessFileName(url, 
InAppBrowserDownloads.this.contentDisposition, 
InAppBrowserDownloads.this.mimetype);
        DownloadManager.Request request = new 
DownloadManager.Request(Uri.parse(url));
    
        request.addRequestHeader("Cookie", cookie);
        request.addRequestHeader("User-Agent", 
InAppBrowserDownloads.this.userAgent);
        request.addRequestHeader("Referer", url);
        request.allowScanningByMediaScanner();
        // Notify client once download is completed!
        
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, 
filename);
    
        try {
            DownloadManager dm = (DownloadManager) 
plugin.cordova.getActivity().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(plugin.cordova.getActivity().getApplicationContext(), 
"Downloading File '" + filename + "'", Toast.LENGTH_LONG).show();
        } catch (Exception exception) {
            
Toast.makeText(plugin.cordova.getActivity().getApplicationContext(), "Error 
downloading file, missing storage permissions", Toast.LENGTH_LONG).show();
            exception.printStackTrace();
        }
    }
    ```
    
    This passes the cookies from the WebView to the DownloadManager, so you're 
able to download files from locations where you need to be signed in.
    I also cleaned up the code a little bit. The important code I added is this 
part:
    ```Java
    import android.webkit.CookieManager;
    [...]
    final String cookie = CookieManager.getInstance().getCookie(url); // Note 
that I defined the variable url before
    [...]
    request.addRequestHeader("Cookie", cookie);
    request.addRequestHeader("User-Agent", 
InAppBrowserDownloads.this.userAgent);
    request.addRequestHeader("Referer", url);
    [...]
    ```


> Opening a link to a file in the inappbrowser on Android crashes the app
> -----------------------------------------------------------------------
>
>                 Key: CB-12834
>                 URL: https://issues.apache.org/jira/browse/CB-12834
>             Project: Apache Cordova
>          Issue Type: Bug
>          Components: cordova-plugin-inappbrowser
>         Environment: Android > 4.4
>            Reporter: Mèir Noordermeer
>            Priority: Critical
>              Labels: android, cordova, download, inappbrowser
>   Original Estimate: 2h
>  Remaining Estimate: 2h
>
> When opening a link to a a file (a PDF, for example) the inappbrowser 
> crashes, because on Android versions of 4.4 storage permissions are not 
> automatically granted anymore. Thus these should be requested instead of 
> letting the app crash.
> I am creating this issue to follow the guidelines of pull requests.



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@cordova.apache.org
For additional commands, e-mail: issues-h...@cordova.apache.org

Reply via email to