How to get file name and real path of Google drive document?

前端 未结 2 620
天命终不由人
天命终不由人 2020-12-18 23:23

I am using the following code to get the file name and path of files in the File Manager. However it does not return a path for Google Drive files. Any idea how to obtain th

2条回答
  •  抹茶落季
    2020-12-18 23:47

    Get file name and real path from Google Drive URI very easily by following steps:

    1. Add file provider path in Android manifest file.

      
      
          
          
          
          
          
              
                  
                      
      
                      
                  
              
              
                  
              
          
      
      
    2. Create xml folder under res and add provider_paths.xml.

      
      
          
          
          
          
      
      
    3. Utils class for access data from Google Drive.

      public class Utils {
          private static Uri contentUri = null;
      
          @SuppressLint("NewApi")
          public static String getPath(final Context context, final Uri uri) {
              // check here to KITKAT or new version
              final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
              // DocumentProvider
              if (isKitKat && DocumentsContract.isDocumentUri(context, uri))
                {
                  // MediaProvider
                   if (isMediaDocument(uri)) {
                       if (isGoogleDriveUri(uri)) {
                      return getDriveFilePath(uri, context);
                  }
      
      
                }
            }
      
    4. isGoogleDriveUri method

      private static boolean isGoogleDriveUri(Uri uri) {
          return "com.google.android.apps.docs.storage".equals(uri.getAuthority()) || "com.google.android.apps.docs.storage.legacy".equals(uri.getAuthority());
      }
      
    5. getDriveFilePath method

      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();
          }     
      }
      
    6. Getting file path in onActivityResult

      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
          super.onActivityResult(requestCode, resultCode, data);
          if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
              if ((data != null) && (data.getData() != null)) {
                  Uri selectedFile = data.getData();
                  if (selectedFile.getLastPathSegment() != null) {
                      //Here you will get File Path
                      String strPath = FileUtils.getPath(this, selectedFile);  
                  }
              }
          }
      }
      

提交回复
热议问题