I have tried numerous methods of getting a file object from a google drive URI but most result in the following error:
java.lang.SecurityException: Permission Denial: reading com.google.android.apps.docs.storagebackend.StorageBackendContentProvider uri content://com.google.android.apps.docs.storage/document/acc=1;doc=4235 from pid=32140, uid=10149 requires android.permission.MANAGE_DOCUMENTS, or grantUriPermission()
So I added android.permission.MANAGE_DOCUMENTS and it did not work, so I tried to use grantUriPermission() and got the following error:
java.lang.SecurityException: Uid 10151 does not have permission to uri 0 @ content://com.google.android.apps.docs.storage/document/acc=1;doc=4235
This file plugin has a nice way of getting files from just about anywhere, except google drive: aFileChooser.
So I took to stack overflow for answers:
Open a Google Drive File Content URI after using KitKat Storage Access Framework
Android KitKat securityException when trying to read from MediaStore
Getting Permission Denial Exception
No permission for UID to access URI error on Android Application
and there are about 15 others, but none helped
I am using Cordova so this is a cross platform app so it restricts me a bit. Here is how I get my URI, I press a button in my webview app, and then a new page is loaded allowing me to select my video from somewhere on my phone and I select google drive, then select a video, and then a URI is sent back to my webview I was on, I then send the URI to a java file to try and download the file. here is the different methods I have tried to download the file:
String filePath = null;
Log.d(TAG,"URI = "+ uri);
if (uri != null && "content".equals(uri.getScheme())) {
Log.d(TAG, "got inside if");
Cursor cursor = context.getContentResolver().query(uri, new String[] { android.provider.MediaStore.Files.FileColumns.DATA }, null, null, null);
cursor.moveToFirst();
filePath = cursor.getString(0);
cursor.close();
} else {
Log.d(TAG, "got inside else");
filePath = uri.getPath();
}
Log.d("","Chosen path = "+ filePath);
This method is similar, but fails the same:
String mimeType = context.getContentResolver().getType(uri);
Log.d(TAG,mimeType);
Cursor returnCursor = getContentResolver().query(returnUri, null, null, null, null);
int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
int sizeIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE);
returnCursor.moveToFirst();
Log.d(TAG,returnCursor.getString(nameIndex));
Log.d(TAG,Long.toString(returnCursor.getLong(sizeIndex)));
This method game the same error as the previous
try{
InputStream input = context.getContentResolver().openInputStream(uri);
try {
File file = new File(context.getCacheDir(), "cacheFileAppeal.mp4");
OutputStream output = new FileOutputStream(file);
try {
try {
byte[] buffer = new byte[4 * 1024]; // or other buffer size
int read;
while ((read = input.read(buffer)) != -1) {
output.write(buffer, 0, read);
}
output.flush();
} finally {
output.close();
}
} catch (Exception e) {
e.printStackTrace(); // handle exception, define IOException and others
}
} finally {
input.close();
}
}
catch(Exception e){
e.printStackTrace();
}
all three game me
java.lang.SecurityException: Permission Denial: reading com.google.android.apps.docs.storagebackend.StorageBackendContentProvider uri content://com.google.android.apps.docs.storage/document/acc=1;doc=4235 from pid=32140, uid=10149 requires android.permission.MANAGE_DOCUMENTS, or grantUriPermission()
The following code for some reason did not crash, but gave me the mimeType:
String mimeType = context.getContentResolver().getType(uri);
I think the problem is that I am not using the URI in the original intent where the URI was retrieved from google drive, but I am not sure.
Does anyone know how to get a file object from google drive URI?