Issue
I have been working to fetch file path from storage till now, For example file path is /storage/emulated/0/Download/NTL_ANDRODI_DOGMA_SYSTEMS_SRL_A_SOCIO_UNICO_TEST NEW.afgclic
After android 11 its unable to fetch FilePath. After giving permission
uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
Its allowing for Android 11 also, but app is getting rejected by Play store. Since the permission cannot be used without a specific reason and its allowed only for filemanager or antivirus app. Now the point is how other app manages to work on Android 11 for fetching files.
I have been using READ and WRITE storage permission to access file from external storage. Files are required to verify license from other library information
Waiting to get the issue resolved. Thanks for the help.
Solution
I have fixed it by fetching path from Google drive
private static String getDriveFilePath(Uri uri, Context context) {
Uri returnUri = uri;
Cursor returnCursor = context.getContentResolver().query(returnUri, null, null, null, null);
/*
* Get the column indexes of the data in the Cursor,
* * move to the first row in the Cursor, get the data,
* * and display it.
* */
int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
int sizeIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE);
returnCursor.moveToFirst();
String name = (returnCursor.getString(nameIndex));
String size = (Long.toString(returnCursor.getLong(sizeIndex)));
File file = new File(context.getCacheDir(), name);
try {
InputStream inputStream = context.getContentResolver().openInputStream(uri);
FileOutputStream outputStream = new FileOutputStream(file);
int read = 0;
int maxBufferSize = 1 * 1024 * 1024;
int bytesAvailable = inputStream.available();
//int bufferSize = 1024;
int bufferSize = Math.min(bytesAvailable, maxBufferSize);
final byte[] buffers = new byte[bufferSize];
while ((read = inputStream.read(buffers)) != -1) {
outputStream.write(buffers, 0, read);
}
Log.e("File Size", "Size " + file.length());
inputStream.close();
outputStream.close();
Log.e("File Path", "Path " + file.getPath());
} catch (Exception e) {
Log.e("Exception", e.getMessage());
}
return file.getPath();
}
}
Answered By - Priyanka Singhal
Answer Checked By - David Goodson (JavaFixing Volunteer)