React Native [Android]
Samsung Phone
Libraries :
Able to :
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With