React Native [Android]使用'React-Native-Document-Picker'和'react-native-get-real-path'从Google Drive获取URI

12

React Native [Android]

三星手机

库:

  • react-native-document-picker [返回我们的URI]
  • react-native-get-real-path [将URI转换为实际路径]

能够:

  • 从本地文件获取URI并获取包括图像在内的实际路径
  • 在选择文件时,能够从Google Drive获取URI

无法 :

  • 将Google Drive URI转换为实际路径

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

}

我的Google Drive链接是这样的:

content://com.google.android.apps.docs.storage/document/acc%3D2%3Bdoc%3D1

你有没有在这方面有什么好运? - Ross Logan
很不幸,我还没有。仍在等待任何建议。在IOS上工作但在Android上不行。 - skrusetvt
1个回答

3

修复了获取谷歌云盘文件绝对路径的 bug。

事实证明,我们无法直接从选择谷歌云盘文件返回的 URI 中获取绝对路径。因此,我们需要应用某种方法来解决这个问题。

我所做的是,将 react-native-get-real-path 存储库分叉成自己的存储库,然后在 GRP.java 文件中进行了一些更改。

基本上,我从获取的谷歌云盘文件的 URI 中创建了一个 InputStream,然后使用该流将文件复制到应用程序的缓存目录,并返回该文件的绝对路径。

以下是解决方案的代码片段:

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;

你可以克隆 git 代码库。参考 https://github.com/Wraptime/react-native-get-real-path/pull/8

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接