来自Google云端硬盘的React Native [Android] URI与'React-Native-Document-Picker'和'react-native-get-real-path'

skr*_*tvt 11 uri react-native react-native-android

React Native [Android]

三星手机

图书馆:

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

能够 :

  • 从本地文件中获取URI并获取包括图像在内的真实路径
  • 我选择文件时能够从Google云端硬盘获取URI

无法:

  • 将Google云端硬盘URI转换为真实路径

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

我的谷歌驱动器的URI是这样的:

content://com.google.android.apps.docs.storage/document/acc%3D2%3Bdoc%3D1
Run Code Online (Sandbox Code Playgroud)

sha*_*nya 3

修复了获取 Google Drive 文件绝对路径的错误。

所以事实证明,我们不能直接URI从选择Google Drive File返回的绝对路径中获取绝对路径。因此我们需要应用某种 hack 来解决这个问题。

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

我基本上是InputStream从获得的谷歌驱动器文件创建的URI,然后使用该流,将文件复制到应用程序的缓存目录中,并返回该文件的绝对路径,瞧。

这是解决方案的代码片段:

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;
Run Code Online (Sandbox Code Playgroud)

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