Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
465 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...