简体   繁体   中英

Not abe to access file in External Storage created by other app

I am trying to read directory inside Documents folder created by my app (App A), the files that is created by my app(eg. Filename: abc.json) I am able to read it but if I created another file say xyz.json by other app let us say File Explorer then I am not able to read it.

I am using Medistore method to read file and in android 11. What should I do?

Code I am using to perform read:

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        if (Environment.isExternalStorageManager()==false) {

            Intent intent = new Intent();
            intent.setAction(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
            Uri uri = Uri.fromParts("package", context.getPackageName(), null);
            intent.setData(uri);
            context.startActivity(intent);


        }
    }


    try{
        Uri contentUri = MediaStore.Files.getContentUri("external");
        String selection = MediaStore.MediaColumns.RELATIVE_PATH + "=?";
        String[] selectionArgs = new String[]{Environment.DIRECTORY_DOCUMENTS + "/"+folderName+"/"};

        Cursor cursor =context.getContentResolver().query(contentUri, null, selection, selectionArgs, null);

        Uri uri = null;

        if (cursor.getCount() == 0) {
            Toast.makeText(context, "No file found in \"" + Environment.DIRECTORY_DOCUMENTS + "/"+folderName+"\""+cursor.getCount(), Toast.LENGTH_LONG).show();
        } else {
            while (cursor.moveToNext()) {
                int filenameTemp=cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME);
                String fileName = cursor.getString(filenameTemp);

                if (fileName.equals(readFileName)) {
                    int idTemp=cursor.getColumnIndex(MediaStore.MediaColumns._ID);
                    long id = cursor.getLong(idTemp);

                    uri = ContentUris.withAppendedId(contentUri, id);

                    break;
                }
            }

            if (uri == null) {
                Toast.makeText(context, "\""+readFileName+"\" not found", Toast.LENGTH_SHORT).show();
            } else {
                try {
                    InputStream inputStream = context.getContentResolver().openInputStream(uri);

                    int size = inputStream.available();

                    byte[] bytes = new byte[size];

                    inputStream.read(bytes);

                    inputStream.close();

                    String jsonString = new String(bytes, StandardCharsets.UTF_8);
                    returnJsonString=jsonString;

                } catch (IOException e) {
                    Toast.makeText(context.getApplicationContext(), "Fail to read file", Toast.LENGTH_SHORT).show();
                }
            }

You have two options.

Request MANAGE_EXTERNAL_STORAGE in manifest.

Let the user select the file with ACTION_OPEN_DOCUMENT .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM