我正在尝试从相机胶卷
上传图片。问题在于,相机胶卷
组件返回的是content:// URI而不是实际的文件路径。为了上传图像,我需要一个文件路径,有没有办法将content:// URI转换为文件URI呢?谢谢。
我正在尝试从相机胶卷
上传图片。问题在于,相机胶卷
组件返回的是content:// URI而不是实际的文件路径。为了上传图像,我需要一个文件路径,有没有办法将content:// URI转换为文件URI呢?谢谢。
RNGRP.getRealPathFromURI(this.state.selectedImageUri).then(path =>
RNFS.readFile(path, 'base64').then(imageBase64 =>
this.props.actions.sendImageAsBase64(imageBase64)
)
)
undefined is not a function(reactNativeGetRealPath2.default.getRealPathFromUri(path))
,而我提供的路径是content://com.google.android.apps.photos.contentprovider/-1/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F890/ORIGINAL/NONE/225703422
。 - P.Lorand你可以使用react-native-fs
的copyFile
方法将内容uri转换为文件uri。
if (url.startsWith('content://')) {
const urlComponents = url.split('/')
const fileNameAndExtension = urlComponents[urlComponents.length - 1]
const destPath = `${RNFS.TemporaryDirectoryPath}/${fileNameAndExtension}`
await RNFS.copyFile(url, destPath)
}
然后你可以按预期使用 'file://' + destPath
content:// URI
传递给下面的方法,将其转换为字符串形式的文件路径,然后使用该文件对象执行任何操作。File file = new File(getURIPath(uriValue));
/**
* URI Value
* @return File Path.
*/
String getURIPath(Uri uriValue)
{
String[] mediaStoreProjection = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(uriValue, mediaStoreProjection, null, null, null);
if (cursor != null){
int colIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String colIndexString=cursor.getString(colIndex);
cursor.close();
return colIndexString;
}
return null;
}