Android 6 gets the path to the downloaded file

In our application (Xamarin C #) we download files from the server. At the end of a successful download, we get the URI for the newly downloaded file and from the URI we get the file path:

Android.Net.Uri uri = downloadManager.GetUriForDownloadedFile(entry.Value); path = u.EncodedPath; 

In Android 4.4.2 and on Android 5, the uri and path are as follows:

 uri="file:///storage/emulated/0/Download/2.zip" path = u.EncodedPath ="/storage/emulated/0/Download/2.zip" 

Then we use the path to process the file. The problem is that in Android 6 (on a real Nexus phone) we get a completely different uri and path :

 uri="content://downloads/my_downloads/2802" path="/my_downloads/2802" 

This breaks my code, raising a FileNotFound exception. Please note that the downloaded file exists and is located in the Downloads folder. How can I use the URI that I get with Android 6 to get the correct file path so that I can in the file and process it?

Thanks, donescamillo@gmail.com

+6
source share
3 answers

I did not receive your actual requirement, but it looks like you want to process the contents of the file. If this can be done by reading the contents of the file using the file descriptor of the downloaded file. Code snippet as

  ParcelFileDescriptor parcelFd = null; try { parcelFd = mDownloadManager.openDownloadedFile(downloadId); FileInputStream fileInputStream = new FileInputStream(parcelFd.getFileDescriptor()); } catch (FileNotFoundException e) { Log.w(TAG, "Error in opening file: " + e.getMessage(), e); } finally { if(parcelFd != null) { try { parcelFd.close(); } catch (IOException e) { } } } 

But I also want to move or delete this file after processing.

+1
source

You can create your URI with the download folder: Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_DOWNLOADS) .toURI ();

0
source

It works. @ 2016.6.24

 @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if(DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals( action)) { DownloadManager downloadManager = (DownloadManager)context.getSystemService(Context.DOWNLOAD_SERVICE); long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0); DownloadManager.Query query = new DownloadManager.Query(); query.setFilterById(downloadId); Cursor c = downloadManager.query(query); if(c != null) { if (c.moveToFirst()) { int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS); if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) { String downloadFileUrl = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI)); startInstall(context, Uri.parse(downloadFileUrl)); } } c.close(); } } } private boolean startInstall(Context context, Uri uri) { if(!new File( uri.getPath()).exists()) { System.out.println( " local file has been deleted! "); return false; } Intent intent = new Intent(); intent.addFlags( Intent.FLAG_ACTIVITY_NEW_TASK); intent.setAction( Intent.ACTION_VIEW); intent.setDataAndType( uri, "application/vnd.android.package-archive"); context.startActivity( intent); return true; } 
0
source

All Articles