首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用React Expo Sharing.shareAsync分享本地照片?

如何使用React Expo Sharing.shareAsync分享本地照片?
EN

Stack Overflow用户
提问于 2020-04-16 08:18:03
回答 2查看 955关注 0票数 0

我正在尝试使用Sharing.shareAsync()在React Expo中共享本地文件。使用MediaLibrary.getAssetInfoAsync()获取照片信息(安卓系统):

代码语言:javascript
复制
"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_ROLLCAMERA权限,app.json包括:

代码语言:javascript
复制
"android": {
      "permissions": [
        "CAMERA",
        "CAMERA_ROLL",
        "READ_EXTERNAL_STORAGE",
        "WRITE_EXTERNAL_STORAGE"
      ]
    }

Expo文档说我应该能够共享本地文件。不知道我做错了什么,也不知道下一步该怎么做。蒂娅。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-04-16 09:28:36

看起来这可能是Sharing应用编程接口中的一个错误。现在,您可以通过将文件复制到您的文档目录,然后从那里进行共享来解决此问题。下面是一个例子:https://snack.expo.io/@notbrent/share-media-library-photo

下面是该示例中的相关代码:

代码语言:javascript
复制
// 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);
票数 3
EN

Stack Overflow用户

发布于 2020-08-17 06:15:33

这是share API的一个问题。你可以使用expo-image-manipulation来解决这个问题。举个例子:

代码语言:javascript
复制
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);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61240694

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档