Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android 11 not fetching path for file

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 it's unable to fetch FilePath. After giving permission

uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"

It's 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.

like image 480
Priyanka Singhal Avatar asked Sep 05 '25 03:09

Priyanka Singhal


1 Answers

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();
}     
}
like image 137
Priyanka Singhal Avatar answered Sep 08 '25 00:09

Priyanka Singhal