Opening files downloaded by DownloadManager causes "The document path is not valid" error

StackOverflow https://stackoverflow.com/questions/22987393

  •  01-07-2023
  •  | 
  •  

質問

I'm trying to open pdf file downloaded by DownloadManager but from time to time I'm getting "The document path is not valid" on Samsung Galaxy S3 with Android 4.1.2

Intent:

Intent openIntent = new Intent(Intent.ACTION_VIEW);
openIntent.setDataAndType(downloadManager.getUriForDownloadedFile(lastDownloadId), "application/pdf");
startActivity(openIntent);

Permissions:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

DownloadManager:

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).mkdirs(); 

DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); 
long lastDownloadId = downloadManager.enqueue(new DownloadManager.Request(uri)
    .setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
    .setAllowedOverRoaming(false)
    .setTitle(title)
    .setDescription(description)
    .setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "myfile.pdf"));

What am I doing wrong?

Edit: From to time I'm getting this:

COLUMN_ID: 108 
COLUMN_BYTES_DOWNLOADED_SO_FAR: 33085
COLUMN_LAST_MODIFIED_TIMESTAMP: 1397152425941 
COLUMN_LOCAL_URI: null
COLUMN_STATUS: 16 COLUMN_REASON: 1008

Why?

役に立ちましたか?

解決

A status of 16 is STATUS_FAILED, meaning that the download failed. A reason of 1008 is ERROR_CANNOT_RESUME, and I have no idea what that means in the real world. But, your file was not downloaded, and hence you have no Uri.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top