Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filename for downloads #66

Closed
thijseend opened this issue Jul 18, 2016 · 4 comments
Closed

Filename for downloads #66

thijseend opened this issue Jul 18, 2016 · 4 comments

Comments

@thijseend
Copy link

Hello,

I've added this webview to my project only there is one thing that doesn't work.
When I try to add the download section in my activity the second filename turns red??

private AdvancedWebView mWebView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mWebView = (AdvancedWebView) findViewById(R.id.webview);
    mWebView.setListener(this, this);
    mWebView.loadUrl("http://myurl");

    // ...
}

@SuppressLint("NewApi")
@Override
protected void onResume() {
    super.onResume();
    mWebView.onResume();
    // ...
}

@SuppressLint("NewApi")
@Override
protected void onPause() {
    mWebView.onPause();
    // ...
    super.onPause();
}

@Override
protected void onDestroy() {
    mWebView.onDestroy();
    // ...
    super.onDestroy();
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
    mWebView.onActivityResult(requestCode, resultCode, intent);
    // ...
}

@Override
public void onBackPressed() {
    if (!mWebView.onBackPressed()) { return; }
    // ...
    super.onBackPressed();
}

@Override
public void onPageStarted(String url, Bitmap favicon) { }

@Override
public void onPageFinished(String url) { }

@Override
public void onPageError(int errorCode, String description, String failingUrl) { }

@Override
public void onDownloadRequested(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
    // some file is available for download
    // either handle the download yourself or use the code below

    final String filename;
    // ...

    if (AdvancedWebView.handleDownload(this, url, filename)) {
        // download successfully handled
    }
    else {
        // download couldn't be handled because user has disabled download manager app on the device
        // TODO show some notice to the user
    }
}

@Override
public void onExternalPageRequest(String url) { }

The word filename gets red in the next part: (this, url, filename)) {

@thijseend
Copy link
Author

Okeee, found out that if i change the first filename in the code it downloads but with the filename i provided.

    final String filename = "cool.mp3";
    // ...

    if (AdvancedWebView.handleDownload(this, url, filename)) {
        // download successfully handled
    }
    else {
        // download couldn't be handled because user has disabled download manager app on the device
        // TODO show some notice to the user
    }
}

but how do i change this so that it gets the filename from the server?

@ocram ocram added the question label Jul 19, 2016
@ocram
Copy link
Contributor

ocram commented Jul 19, 2016

Thanks for your question!

Glad you've figured out the solution to your original problem already. The code that you copied from the README was indeed not complete yet. The variable declaration for the filename variable was there,. but the assignment was not. That's what the // ... comment was to represent. So we should probably improve the example to make this absolutely clear.

Regarding your new question, i.e. how to determine the filename, you got this right as well: Currently, the intended solution is that you hard-code the filename as you did or build the filename with some conditional structures, etc.

If you want this to work automatically, #32 is probably related. The URLUtil.guessFileName method may be what you're looking for. Can you try it? You should have all the "ingredients" already because the required parameters should be what you've received in the onDownloadRequested method.

Hope that helps! If it works, we can add that feature to this library natively.

@thijseend
Copy link
Author

Heey,

I've found the solution to my problem:

String filename = URLUtil.guessFileName(url, contentDisposition, mimetype);
Now it works very well!

@ocram
Copy link
Contributor

ocram commented Jul 24, 2016

Yes, that's what we proposed. Thanks for testing!

Built-in support for this has been added in d5cb1e5 now. In order to use this, just update the dependency as shown in the README and follow the migration guide from Migration.md.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants