DownloadManager COLUMN_LOCAL_FILENAME deprecated

techtinkerer picture techtinkerer · Aug 9, 2016 · Viewed 8.4k times · Source

On Android N, i am getting an exception. It is a known issue per the documentation, which asks me to use ContentResolver.openFileDescriptor()

https://developer.android.com/reference/android/app/DownloadManager.html#COLUMN_LOCAL_FILENAME

I am not sure how to use. Where is the ContentResolver object here that I can use to get the filename? I never used it. So, I will appreciate any help.

08-04 11:20:59.765 7010 7290 W System.err: java.lang.SecurityException: COLUMN_LOCAL_FILENAME is deprecated; use ContentResolver.openFileDescriptor() instead

08-04 11:20:59.765 7010 7290 W System.err: at android.app.DownloadManager$CursorTranslator.getString(DownloadManager.java:1499)

Here is my code.

    DownloadManager.Query query = new DownloadManager.Query();
    query.setFilterById(id);
    Cursor cursor = downloadManager.query(query);

    final String downloadFilePath = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME));
    cursor.close();

I tried the downlaodManager.getFileUri, but isn't what I am looking for. Appreciate any help.

Thanks

Answer

techtinkerer picture techtinkerer · Aug 9, 2016

The following is working for me now:

    String downloadFilePath = null;
    String downloadFileLocalUri = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
    if (downloadFileLocalUri != null) {
        File mFile = new File(Uri.parse(downloadFileLocalUri).getPath());
        downloadFilePath = mFile.getAbsolutePath();
    }