26

I am using the Storage Access Framework for android 4.4 and opening the file picker.

Everything works except when choosing a file from Google Drive, I can only figure out how to open it as an input stream, but I would like to get a java File object.

The content uri that's being returned looks something like this: content://com.google.android.apps.docs.storage/document/acc%3D4%3Bdoc%3D2279

Other questions that are similar but do not have a working solution which allows me to get the filename, filesize and contents:

I've also looked into Paul Burke's FileChooser ( https://github.com/iPaulPro/aFileChooser) and this is the most common question on the issue's list.

How can I get a file from that content uri?

My current workaround is to write out a temporary file from an inputstream.

Thanks!

Community
  • 1
  • 1
Keith Entzeroth
  • 2,049
  • 1
  • 18
  • 22

3 Answers3

39

Ok. I found that the right way is to use the input stream from the other posts in conjunction with some data from the contentresolver.

For reference here are the hard to find android docs: https://developer.android.com/training/secure-file-sharing/retrieve-info.html

The relevant code to get mimetype, filename, and filesize:

Uri returnUri = returnIntent.getData();
String mimeType = getContentResolver().getType(returnUri);
Cursor returnCursor =
        getContentResolver().query(returnUri, null, null, null, null);
int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
int sizeIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE);
returnCursor.moveToFirst();
TextView nameView = (TextView) findViewById(R.id.filename_text);
TextView sizeView = (TextView) findViewById(R.id.filesize_text);
nameView.setText(returnCursor.getString(nameIndex));
sizeView.setText(Long.toString(returnCursor.getLong(sizeIndex)));

And to get the file contents:

getContentResolver().openInputStream(uri)

Hope this helps someone else.

cgogolin
  • 960
  • 1
  • 10
  • 22
Keith Entzeroth
  • 2,049
  • 1
  • 18
  • 22
  • got same issue, http://stackoverflow.com/questions/30206483/converting-bitmap-from-uri-is-returning-null/30206736#30206483 – Madhu May 13 '15 at 06:52
  • kindly look it out, give some solution – Madhu May 13 '15 at 06:52
  • Its only getting file name but i want to get the content uri of google drive file – Chandan Wadhwa Jul 04 '16 at 12:58
  • 2
    You find the mimeType, but you don't use it. You call ".openInputStream" with the parameter "uri", and you haven't told us how you got that variable. Some more information would be appreciated.. – Moonbloom Feb 22 '17 at 15:00
  • @Moonbloom when you know the file name you can create an empty file `File.createTempFile(String prefix, String suffix)`, and then copy the InputStream to this file. https://stackoverflow.com/questions/11501418/is-it-possible-to-create-a-file-object-from-inputstream – Weiyi Jan 04 '19 at 02:37
  • @Keith Entzeroth What was the variable 'uri' in getContentResolver().openInputStream(uri).Is it returnUri ??Pls reply – KJEjava48 Feb 02 '22 at 06:09
3

Adding to @Keith Entzeroth answer , after getting fileName, fileSize and Input Stream , this is way to get the file

 public static File getFile(final Context context, final Uri uri) {
        Log.e(TAG,"inside getFile==");
        ContentResolver contentResolver = context.getContentResolver();
        try {
            String mimeType = contentResolver.getType(uri);
            Cursor returnCursor =
                    contentResolver.query(uri, null, null, null, null);
            int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
            int sizeIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE);
            returnCursor.moveToFirst();
            String fileName = returnCursor.getString(nameIndex);
            String fileSize = Long.toString(returnCursor.getLong(sizeIndex));
            InputStream inputStream =  contentResolver.openInputStream(uri);


            File tempFile = File.createTempFile(fileName, "");
            tempFile.deleteOnExit();
            FileOutputStream out = new FileOutputStream(tempFile);
            IOUtils.copyStream(inputStream,out);
            return tempFile;

          }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
B.shruti
  • 1,589
  • 1
  • 21
  • 41
0

this code solved my problem, when i tried to select image from google drive ,app get crashed ,

 private void setImagePath(Intent data) throws Exception {

        String wholeID="";
        Uri selectedImage = data.getData();
        if(Build.VERSION.SDK_INT<=Build.VERSION_CODES.JELLY_BEAN_MR2){
            wholeID=getUriPreKitkat(selectedImage);
        }else  {
            wholeID = DocumentsContract.getDocumentId(selectedImage);

        }

        // Split at colon, use second item in the array
        Log.i("debug","uri google drive "+wholeID);
        String id = wholeID.split(":")[1];

        String[] column = {MediaStore.Images.Media.DATA};

        // where id is equal to
        String sel = MediaStore.Images.Media._ID + "=?";

        Cursor cursor = getActivity().getContentResolver().
                query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                        column, sel, new String[]{id}, null);


        int columnIndex = cursor.getColumnIndex(column[0]);

        if (cursor.moveToFirst()) {
            filePath = cursor.getString(columnIndex);
        }
        cursor.close();

    }
Adeeb karim
  • 292
  • 3
  • 9