get a file object from google drive with URI android
<p>I have tried numerous methods of getting a file object from a google drive URI but most result in the following error:</p>

<pre><code>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() 
</code></pre>

<p>So I added android.permission.MANAGE_DOCUMENTS and it did not work, so I tried to use grantUriPermission() and got the following error:</p>

<pre><code>java.lang.SecurityException: Uid 10151 does not have permission to uri 0 @ content://com.google.android.apps.docs.storage/document/acc=1;doc=4235 
</code></pre>

<p>This file plugin has a nice way of getting files from just about anywhere, except google drive: <a href="https://github.com/iPaulPro/aFileChooser" rel="nofollow noreferrer">aFileChooser</a>. </p>

<p>So I took to stack overflow for answers:</p>

<p><a href="https://stackoverflow.com/questions/25171246/open-a-google-drive-file-content-uri-after-using-kitkat-storage-access-framework">Open a Google Drive File Content URI after using KitKat Storage Access Framework</a></p>

<p><a href="https://stackoverflow.com/questions/19837358/android-kitkat-securityexception-when-trying-to-read-from-mediastore">Android KitKat securityException when trying to read from MediaStore</a></p>

<p><a href="https://stackoverflow.com/questions/22178041/getting-permission-denial-exception" title="Getting Permission Denial Exception">Getting Permission Denial Exception</a></p>

<p><a href="https://stackoverflow.com/questions/33032902/no-permission-for-uid-to-access-uri-error-on-android-application">No permission for UID to access URI error on Android Application</a></p>

<p>and there are about 15 others, but none helped</p>

<p>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:</p>

<pre><code>String filePath = null;
Log.d(TAG,"URI = "+ uri);                                       
if (uri != null &amp;&amp; "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);
</code></pre>

<p>This method is similar, but fails the same:</p>

<pre><code>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)));
</code></pre>

<p>This method game the same error as the previous</p>

<pre><code>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();
}
</code></pre>

<p>all three game me </p>

<pre><code>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() 
</code></pre>

<p>The following code for some reason did not crash, but gave me the mimeType:</p>

<pre><code>String mimeType = context.getContentResolver().getType(uri);
</code></pre>

<p>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.</p>

<p>Does anyone know how to get a file object from google drive URI?</p>