1
votes

Now the google drive api is deprecated , so i have integrated their new Drive REST API . In my app i want to show file picker to pick doc/images from google drive . I have used their sample : https://github.com/gsuitedevs/android-samples/blob/master/drive/deprecation/app/src/main/java/com/google/android/gms/drive/sample/driveapimigration/MainActivity.java

private void openFileFromFilePicker(Uri uri) {

    if (mDriveServiceHelper != null) {
        Log.d(TAG, "Opening " + uri.getPath());

        mDriveServiceHelper.openFileUsingStorageAccessFramework(getContentResolver(), uri)
                .addOnSuccessListener(nameAndContent -> {
                        String name = nameAndContent.first;
                        String content = nameAndContent.second;

                        mFileTitleEditText.setText(name);
                        mDocContentEditText.setText(content);

                        // Files opened through SAF cannot be modified.
                        setReadOnlyMode();
                    })
                .addOnFailureListener(exception ->
                        Log.e(TAG, "Unable to open file from picker.", exception));
    }
}

Now i want to upload that selected file on my server for that i would need a fileID, but in onActivityResult i can only access file name and content . So please help me and suggest me any solution . How can i upload selected resume file or image from google drive to my server .

1

1 Answers

0
votes

Perhaps you have already found the answer to your question, but the answer was already in your question. You use the Storage Access Framework (SAF). When the file is opened, the content of the file can be downloaded to the UI. There should be an additional piece of code - a continuation of this, where the content is loaded.

/**
 * Opens the file at the {@code uri} returned by a Storage Access Framework {@link 
 Intent}
 * created by {@link #createFilePickerIntent()} using the given {@code contentResolver}.
 */
public Task<Pair<String, String>> openFileUsingStorageAccessFramework(
        ContentResolver contentResolver, Uri uri) {
    return Tasks.call(mExecutor, () -> {
            // Retrieve the document's display name from its metadata.
            String name;
            try (Cursor cursor = contentResolver.query(uri, null, null, null, null)) {
                if (cursor != null && cursor.moveToFirst()) {
                    Log.d(TAG, "cursor.getColumnCount(): " + cursor.getColumnCount());
                    for (int i = 0; i < cursor.getColumnCount(); i++) {
                        Log.d(TAG, i + " - " + cursor.getString(i) + "\n");
                    }
                    int nameIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
                    name = cursor.getString(nameIndex);
                } else {
                    throw new IOException("Empty cursor returned for file.");
                }
            }

            // Read the document's contents as a String.
            String content;
            try (InputStream is = contentResolver.openInputStream(uri);
                 BufferedReader reader = new BufferedReader(new InputStreamReader(is))) {
                StringBuilder stringBuilder = new StringBuilder();
                String line;
                while ((line = reader.readLine()) != null) {
                    stringBuilder.append(line);
                }
                content = stringBuilder.toString();
            }

            return Pair.create(name, content);
        });
}

Look under the comment // Read the document's contents as a String.

About SAF: https://developer.android.com/guide/topics/providers/document-provider

P.S. SAF does not provide access to file ID to download file. SAF has access to file by uri.