[英]How to share a local photo using React Expo Sharing.shareAsync?
我正在嘗試使用Sharing.shareAsync()
在 React Expo 中共享本地文件。 使用MediaLibrary.getAssetInfoAsync()
檢索照片信息(在 Android 上):
"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.
應用具有CAMERA_ROLL
和CAMERA
權限,app.json 包括:
"android": {
"permissions": [
"CAMERA",
"CAMERA_ROLL",
"READ_EXTERNAL_STORAGE",
"WRITE_EXTERNAL_STORAGE"
]
}
Expo 文檔說我應該能夠共享本地文件。 不知道我做錯了什么或接下來要嘗試什么。 TIA。
看起來這可能是Sharing
API 中的錯誤。 您現在可以通過將文件復制到您的文檔目錄然后從那里共享來解決它。 這是一個例子: 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);
這是共享 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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.