Do we need permission to get all files that is self-created by the app after the app is reinstalled in AndroidQ?

Viewed 1098

Use-Case:

Here, I can create and save files in local storage using MediaStore and get all the files from MediaStore. But Once I clear-storage or reinstall the app, the files will no longer be available which was self-created by that same app. Do we need permission to read the self-created files once the app is reinstalled?

If we need permission, how to ask permission and get all that PDF files from that Downloads folder in Android-Q.

public static ArrayList<FileModel> getExternalPDFFileList(Context context) {
    ArrayList<FileModel> uriList = new ArrayList<>();
    try {
        ContentResolver cr = context.getContentResolver();

        String[] projection = {MediaStore.Files.FileColumns._ID, MediaStore.Files.FileColumns.DISPLAY_NAME};
        String selection = MediaStore.Files.FileColumns.MEDIA_TYPE + "="
                + MediaStore.Files.FileColumns.MEDIA_TYPE;
        String[] selectionArgs = null;
        String selectionMimeType = MediaStore.Files.FileColumns.MIME_TYPE + "=?";
        String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension("pdf");
        String[] selectionArgsPdf = new String[]{mimeType};

        String sortOrder = android.provider.MediaStore.Files.FileColumns.DATE_TAKEN + " DESC";
        Cursor cursor = cr.query(extUri, projection, selectionMimeType, selectionArgsPdf, sortOrder + " DESC");

        assert cursor != null;
        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
            int columnIndex = cursor.getColumnIndex(projection[0]);
            long fileId = cursor.getLong(columnIndex);
            Uri fileUri = Uri.parse(extUri.toString() + "/" + fileId);
            String displayName = cursor.getString(cursor.getColumnIndex(projection[1]));
            uriList.add(new FileModel(displayName, fileUri));
        }
        cursor.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return uriList;
}

Note: Before android Q, we can get all the files of the external storage once the permission is enabled. But after Android SDK-28, there is completely different file-system. Due to lack of proper documentation it is difficult to do minor task.

2 Answers

Do we need permission to read the self-created files once the app is reinstalled?

Wrong question.

If you deinstall your app and then reinstall it it is considered a different app.

Hence your app cannot 'see' the files in the media store that were created by the deinstalled app.

As there are no permissions you can do nothing.

Yes as far permissions go, every kind of permission given with a clear-storage is "cleared" also.

Did you check if the files are actually removed after the uninstall? My best guess is that they are if they are saved in the internal storage

Related