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

标签 react-native uri react-native-android

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);
        })
    
    }
    

  • 我来自谷歌驱动器的 URI 是这样的:
    content://com.google.android.apps.docs.storage/document/acc%3D2%3Bdoc%3D1
    

    最佳答案

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

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

    我所做的是,我 fork 了 react-native-get-real-path repo 到我们自己的,然后在 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;
    

    您可以克隆 git repository . https://github.com/Wraptime/react-native-get-real-path/pull/8的引用.

    关于react-native - 来自 Google Drive 的 React Native [Android] URI w/'React-Native-Document-Picker' & 'react-native-get-real-path',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50030622/

    相关文章:

    javascript - 如何以编程方式取消 AlertIOS

    react-native-navigation 底部标签样式

    c# - 从 Host.TopLevelDomain C# 获取有效的 web url

    c# - 是否可以创建一个非绝对的 Uri?

    Android从Uri获取文件的正确方法

    react-native - 如何修复 : A problem occurred configuring project

    react-native-android - react 原生按钮中的阴影

    react-native - 安装 react-redux 后,native_modules.gradle 文件将被删除

    react-native - 向API请求formData,在上传图片时在axios中获取 “Network Error”

    android - 运行 React-native 的 AwsomeProject 时调试目录不存在错误