如何使用 React ExpoSharing.shareAsync 分享本地照片?

Dav*_*ora 0 react-native expo

我正在尝试使用 React Expo 共享本地文件Sharing.shareAsync()。使用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,
Run Code Online (Sandbox Code Playgroud)

打电话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"
      ]
    }
Run Code Online (Sandbox Code Playgroud)

世博文档说我应该能够共享本地文件。不确定我做错了什么或下一步要尝试什么。TIA。

小智 5

这是共享 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);
}
Run Code Online (Sandbox Code Playgroud)