Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 the actual path?

My code -

public String getFilePath() {
    if (uri.getScheme().equalsIgnoreCase("file")) {
        return uri.getLastPathSegment();
    }

    cursorLoader.setUri(uri);
    cursorLoader.setProjection(projections);
    Cursor cursor = cursorLoader.loadInBackground();
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DATA);
    cursor.moveToFirst();
    String realPath = cursor.getString(column_index);
    cursor.close();

    if (realPath == null || realPath.isEmpty()) {
        return null;
    }

return null;
}
like image 642
Suchi Avatar asked Apr 14 '14 15:04

Suchi


2 Answers

You have to use the URI. Through the URI you can getContentResolver.query(theUriThatYouHave, null, null, null, null). Now that you have a cursor, you can check column names etc.

For Google drive there is a column name _display_name. This will give you the file name.

Now you want access to the file? You can open an InputStream to the URI via getContentResolver().openInputStream(theUriThatYouHave).

like image 77
clockwerk Avatar answered Sep 21 '22 21:09

clockwerk


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

  1. Add file provider path in Android manifest file.

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.demo.filemangerdemo">
        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.STORAGE" />
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity android:name="com.demo.filemangerdemo.activity.MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <provider
                android:name="android.support.v4.content.FileProvider"
                android:authorities="${applicationId}.provider"
                android:exported="false"
                android:grantUriPermissions="true">
                <meta-data
                    android:name="android.support.FILE_PROVIDER_PATHS"
                    android:resource="@xml/provider_paths"/>
            </provider>
        </application>
    </manifest>
    
  2. Create xml folder under res and add provider_paths.xml.

    <?xml version="1.0" encoding="utf-8"?>
    <paths xmlns:android="http://schemas.android.com/apk/res/android">
        <cache-path
            name="my_cache"
            path="." />
        <cache-path
            name="cache"
            path="." />
        <external-cache-path
            name="external_cache"
            path="." />
        <files-path
            name="files"
            path="." />
    </paths>
    
  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);  
                }
            }
        }
    }
    
like image 36
satyawan hajare Avatar answered Sep 24 '22 21:09

satyawan hajare