5

I have one problem. I am developing some Streaming/Video downloading app. I am using DownloadManager for video downloads.

Request is to offer user's to pick destination for saving (SDCard or internal phone storage).

I have problems on Lollipop with SAF and DownloadManager.

I am offering to the user to pick directory for storing files I download:

Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
startActivityForResult(intent, REQUEST_CODE_FOR_DESTINATION);

and in onActivityResult i am getting it via

Uri treeUri = data.getData();
DocumentFile destinationDirUri = DocumentFile.fromTreeUri(this, treeUri);

Now, I need to set this destination as destination to DownloadManager.Request

Request request = new DownloadManager.Request(Uri.parse(getDownloadUrl()));
request.setTitle(downloadInfo.getTitle());
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationUri(destinationDirUri);

After I select "Downloads/video" directory, and perform

DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
downloadManager.enqueue(request);

I am getting

E/RecordDownloader﹕ Error while retrieving download infos    
W/System.err﹕ java.lang.IllegalArgumentException: Not a file URI: content://com.android.externalstorage.documents/tree/primary%3ADownload%2Fvideo
W/System.err﹕ at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:167)
W/System.err﹕ at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137)
W/System.err﹕ at android.content.ContentProviderProxy.insert(ContentProviderNative.java:475)
W/System.err﹕ at android.content.ContentResolver.insert(ContentResolver.java:1207)
W/System.err﹕ at android.app.DownloadManager.enqueue(DownloadManager.java:946)
W/System.err﹕ at ch.teleboy.download.RecordDownloader.enqueueDownloadRequest(RecordDownloader.java:102)
W/System.err﹕ at ch.teleboy.download.RecordDownloader.access$300(RecordDownloader.java:48)
W/System.err﹕ at ch.teleboy.download.RecordDownloader$DownloaderTask.run(RecordDownloader.java:191)
W/System.err﹕ at java.lang.Thread.run(Thread.java:818)

If I try to write some file to selected location, it works.

DocumentFile newFile = destinationDirUri.createFile("text/plain", "test.txt");
OutputStream out = getContentResolver().openOutputStream(newFile.getUri());
out.write("Some dummy text...".getBytes());
out.close()

Any suggestions?

Bozic Nebojsa
  • 3,606
  • 1
  • 19
  • 17

0 Answers0