React Native [Android] URI from Google Drive w/ 'React-Native-Document-Picker' & 'react-native-get-real-path'
React Native [Android] URI from Google Drive w/ 'React-Native-Document-Picker' & 'react-native-get-real-path'
React Native [Android]
三星Phone
图书馆:
- react-native-document-picker [returns 我们的 URI]
- react-native-get-real-path [将 URI 转换为真实路径]
能够:
- 从本地文件获取 URI 并获取包含图像的真实路径
- 当我 select 一个文件
时,能够从 Google 驱动器获取 URI
无法:
将 Google 驱动 URI 转换为真实路径
DocumentPicker.show({filetype: [DocumentPickerUtil.allFiles()],},(error,res) => {
RNGRP.getRealPathFromURI(path).then(function(androidPath){
console.log('AndroidPath : ', androidPath);
})
}
我来自 google 驱动器的 URI 是这样的:
content://com.google.android.apps.docs.storage/document/acc%3D2%3Bdoc%3D1
修复了获取 Google 驱动文件绝对路径的错误。
所以事实证明我们不能直接从选择Google Drive File返回的URI
中获取绝对路径。因此,我们需要应用某种技巧来解决问题。
我所做的是,我将 react-native-get-real-path
存储库分叉到我们自己的存储库中,然后在 GRP.java
文件中更改了一些内容。
我基本上从获得的 google 驱动文件的 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 repository. Reference of https://github.com/Wraptime/react-native-get-real-path/pull/8。
React Native [Android]
三星Phone
图书馆:
- react-native-document-picker [returns 我们的 URI]
- react-native-get-real-path [将 URI 转换为真实路径]
能够:
- 从本地文件获取 URI 并获取包含图像的真实路径
- 当我 select 一个文件 时,能够从 Google 驱动器获取 URI
无法:
将 Google 驱动 URI 转换为真实路径
DocumentPicker.show({filetype: [DocumentPickerUtil.allFiles()],},(error,res) => { RNGRP.getRealPathFromURI(path).then(function(androidPath){ console.log('AndroidPath : ', androidPath); }) }
我来自 google 驱动器的 URI 是这样的:
content://com.google.android.apps.docs.storage/document/acc%3D2%3Bdoc%3D1
修复了获取 Google 驱动文件绝对路径的错误。
所以事实证明我们不能直接从选择Google Drive File返回的URI
中获取绝对路径。因此,我们需要应用某种技巧来解决问题。
我所做的是,我将 react-native-get-real-path
存储库分叉到我们自己的存储库中,然后在 GRP.java
文件中更改了一些内容。
我基本上从获得的 google 驱动文件的 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 repository. Reference of https://github.com/Wraptime/react-native-get-real-path/pull/8。