Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native [Android] URI from Google Drive w/ 'React-Native-Document-Picker' & 'react-native-get-real-path'

React Native [Android]

Samsung Phone

Libraries :

  • react-native-document-picker [ returns our URI]
  • react-native-get-real-path [ converts URI to real path]

Able to :

  • Get a URI from local files and get real path including images
  • Able to get URI from Google Drive when I select a file

Unable :

  • Convert Google Drive URI to real path

    DocumentPicker.show({filetype: [DocumentPickerUtil.allFiles()],},(error,res) => {
        RNGRP.getRealPathFromURI(path).then(function(androidPath){
            console.log('AndroidPath : ', androidPath);
        })
    
    }
    

my URI from google drive is like so :

content://com.google.android.apps.docs.storage/document/acc%3D2%3Bdoc%3D1
like image 941
skrusetvt Avatar asked Apr 25 '18 20:04

skrusetvt


1 Answers

Fixed bug to get absolute path of Google Drive File.

So it turns out that we cannot directly get the absolute path from the URI that has been returned by selecting Google Drive File. Hence we need to apply some sort of hack to solve the problem.

What I did was, I forked the react-native-get-real-path repo into our own and then changed few things in GRP.java file.

I basically created InputStream from obtained google drive file's URI and then, using that stream, copied the file into the app's cache directory and returned the absolute path to that file and voila.

Here is the code snippet for the solution:

input = context.getContentResolver().openInputStream(uri);
/* save stream to temp file */
/* displayName is obtained from the URI */
File file = new File(context.getCacheDir(), displayName);
OutputStream output = new FileOutputStream(file);
byte[] buffer = new byte[4 * 1024]; // or other buffer size
int read;

while ((read = input.read(buffer)) != -1) {
  output.write(buffer, 0, read);
}
output.flush();

final String outputPath = file.getAbsolutePath();
return outputPath;

You can clone the git repository. Reference of https://github.com/Wraptime/react-native-get-real-path/pull/8.

like image 88
shah chaitanya Avatar answered Oct 27 '22 19:10

shah chaitanya