3

I'm using android default file picker for my application. All works fine excepts download folder. In downloads folder. Files from both SD card and internal storage are merged and showed. Now the issue is when a file from internal storage it is working perfectly but when the file is from sd card, its returning a wrong file path containing internal storage download path. Does anyone face this issue? Have anyone any solution for this? Thank you!

Here's my implementation.

if (isDownloadsDocument(uri)) {

     final String id = DocumentsContract.getDocumentId(uri);
     if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
         return getDisplayNameColumn(context, uri, null, null);
     }

     final Uri contentUri = ContentUris.withAppendedId(
         Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));

     return getDataColumn(context, contentUri, null, null);
}

public static String getDisplayNameColumn(Context context, Uri uri, String selection,
                                       String[] selectionArgs) {

        Cursor cursor = null;
        final String column = MediaStore.MediaColumns.DISPLAY_NAME;
        final String[] projection = {
                column
        };

        try {
            cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
                    null);
            if (cursor != null && cursor.moveToFirst()) {
                try{
                    final int index = cursor.getColumnIndexOrThrow(column);
                    String fileName = cursor.getString(index);
                    String path = Environment.getExternalStorageDirectory().toString() + "/Download/" + fileName;

                    if(!TextUtils.isEmpty(path)){
                        return path;
                    }
                } catch (IllegalArgumentException ie) {

                }
            }
        }finally {
            if (cursor != null)
                cursor.close();
        }
        return null;
}

public static String getDataColumn(Context context, Uri uri, String selection,
                                       String[] selectionArgs) {

        Cursor cursor = null;
        final String column = "_data";
        final String[] projection = {
                column
        };

        try {
            cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
                    null);
            if (cursor != null && cursor.moveToFirst()) {
                try{
                    final int index = cursor.getColumnIndexOrThrow(column);
                    return cursor.getString(index);
                } catch (IllegalArgumentException ie) {

                }
            }
        } finally {
            if (cursor != null)
                cursor.close();
        }
        return null;
}

Tried parsing from my_downloads and all_downloads, its not working.

0 Answers0