react-native - 来自 Google Drive 的 React Native [Android] URI w/ 'React-Native-Document-Picker' & 'react-native-get-real-path'
问题描述
反应原生 [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 来解决问题。
我所做的是,我将react-native-get-real-path
repo 分叉到我们自己的 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 存储库。https://github.com/Wraptime/react-native-get-real-path/pull/8的参考。
推荐阅读
- gmail-api - Google Gmail API v1 - 设置 SendAs 时出错
- reactjs - 如何使用带有对象的 React 状态设置 Typescript?
- dart - Dart 中的通用包而不是库?
- linux - 通过路径查找文件并使用终端在Linux中使用C语言执行它
- javascript - 将项目添加到对象数组中的第一个对象
- eclipse - 在eclipse暗模式下将包目录更改为黑色
- python-3.x - Kick Start - 18 - G 轮 - Product Triplets - Time_Limit_Exceeded
- algorithm - 当我们处理非常大的数据时,何时使用布隆过滤器以及何时使用位图?
- c# - 如何将记录从一个表插入到另一个表中,列数不同
- javascript - 如何使用 JQuery 或 Js 将值发布到输入框中?