简体   繁体   中英

File download using Android downloads provider

I am trying to use webview to download an email attachment in my application. I follow the procedure that the default browser application follows as below: 1) Write MyWebViewClient that implements DownloadListener and override onDownloadStart. webview calls setDownloadListener passing instance of MyWebViewClient 2) To download, I create instance of ContentValues, fillit up with values as follows:

    ContentValues values = new ContentValues();
    values.put("uri", webAddress.toString());
    values.put("cookiedata", cookies);
    values.put("useragent", userAgent);
    values.put("notificationpackage",
           getPackageName());
    values.put("notificationclass",
            DownloadReceiver.class.getCanonicalName());

    values.put("visibility", 0);
    values.put("mimetype", mimetype);
    values.put("hint", filename);
    values.put("description", webAddress.mHost);
    values.put("is_public_api", Boolean.TRUE);
    values.put("destination", 4);
    if (contentLength > 0) {
        values.put("total_bytes", contentLength);
    }        

    final Uri contentUri =
            getContentResolver().insert(Uri.parse("content://downloads/my_downloads"), values);

Note that as I cannot import android.provider.Downloads, therefore I have to provide actual string values from Downloads.java as above. Somehow download is not working with this code. Also, I had to put value for "is_public_api" and also set "destination" as program goes through checkInsertPermissions function of DownloadProvider.java which uses enforceAllowedValues to check these values. I cannot put the destination to external sdcard where I would like to download. I see that default Android browser is not setting the values for "is_public_api" and "destination" (see BrowserActivity.java). How does that work for default browser? If I don't put value for these values then I get Security Exception.

I have setup "android.permission.ACCESS_DOWNLOAD_MANAGER" permission in my manifest.

您还具有写入清单中设置的SD卡的权限吗?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM