我正在尝试使用Sharing.shareAsync()在React Expo中共享本地文件。使用MediaLibrary.getAssetInfoAsync()获取照片信息(安卓系统):
"filename": "IMG_20200414_190459.jpg",
"height": 2074,
"id": "896",
"localUri": "file:///storage/emulated/0/DCIM/Camera/IMG_20200414_190459.jpg",
"location": null,
"mediaType": "photo",
"modificationTime": 1586905500000,
"uri": "file:///storage/emulated/0/DCIM/Camera/IMG_20200414_190459.jpg",
"width": 4608,调用Sharing.shareAsync(photo.localUri, {mimeType: 'image/jpeg'}时,我得到了错误Failed to share the file: Failed to find configured root that contains /storage/emulated/0/DCIM/Camera/IMG_20200414_190459.jpg。因此,我尝试删除file:后面的一个斜杠,并得到错误Not allowed to read file under given URL.
App具有CAMERA_ROLL和CAMERA权限,app.json包括:
"android": {
"permissions": [
"CAMERA",
"CAMERA_ROLL",
"READ_EXTERNAL_STORAGE",
"WRITE_EXTERNAL_STORAGE"
]
}Expo文档说我应该能够共享本地文件。不知道我做错了什么,也不知道下一步该怎么做。蒂娅。
发布于 2020-04-16 09:28:36
看起来这可能是Sharing应用编程接口中的一个错误。现在,您可以通过将文件复制到您的文档目录,然后从那里进行共享来解决此问题。下面是一个例子:https://snack.expo.io/@notbrent/share-media-library-photo
下面是该示例中的相关代码:
// Placeholder for getting asset from MediaLibrary
let results = await MediaLibrary.getAssetsAsync({ first: 1 });
let asset = results.assets[0];
// Use FileSystem to copy the image from its original location to the app document directory
let assetUriParts = asset.uri.split("/");
let assetName = assetUriParts[assetUriParts.length - 1];
let uri = `${FileSystem.documentDirectory}/${assetName}`;
await FileSystem.copyAsync({
from: asset.uri,
to: uri,
});
// Share the image from the uri that you copied it to
Sharing.shareAsync(uri);发布于 2020-08-17 06:15:33
这是share API的一个问题。你可以使用expo-image-manipulation来解决这个问题。举个例子:
import * as ImageManipulator from "expo-image-manipulator";
const openShareDialogAsync = async () => {
let imageProc = await ImageManipulator.manipulateAsync(yourImageUri);
// this returns an object including the uri
await Sharing.shareAsync(imageProc.uri);
}https://stackoverflow.com/questions/61240694
复制相似问题