Open a Google Drive File Content URI after using KitKat Storage Access Framework

后端 未结 3 450
野趣味
野趣味 2020-12-04 10:11

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 figur

3条回答
  •  南方客
    南方客 (楼主)
    2020-12-04 11:07

    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;
            }
        }
    

提交回复
热议问题