react-native android MediaDocuments提供者uri读取文件错误

时间:2018-02-09 01:16:16

标签: android image react-native permissions provider

我在react-native-document-picker应用程序开发中使用react-native组件让用户选择图像文件。我找回了表示用户选择的文件的uri。然后,当我尝试通过Image.getSize()组件函数获取图像大小时,运行react-native log-android的控制台上会显示以下错误:

Failed to get size for image: content://com.android.providers.media.documents/document/image:298

深入挖掘我使用adb logcat运行控制台以查找以下错误:

E DatabaseUtils: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaDocumentsProvider uri content://com.android.providers.media.documents/document/image:298 from pid=3785, uid=10075 requires android.permission.MANAGE_DOCUMENTS, or grantUriPermission()

我已在android/app/src/main/AndroidManifest.xml文件中拥有以下权限:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

添加<uses-permission android:name="android.permission.MANAGE_DOCUMENTS" />没有任何区别。

那么,使用uri访问图像文件数据的正确方法是什么?

简化代码示例:

import React, { Component } from 'react';
import {
  View,
  Text,
  TouchableOpacity,
  Image,
} from 'react-native';
import { DocumentPicker, DocumentPickerUtil } from 'react-native-document-picker';

export default class ConsultationForm extends Component<{}> {
  _filePicker = () => {
    DocumentPicker.show({
      filetype: [DocumentPickerUtil.images()],
    }, (error,res) => {
      Image.getSize(decodeURIComponent(res.uri), (width, height) => {
        console.log({width: width, height: height});
      });
    });
  };

  render() {
    return (
      <View>
        <TouchableOpacity onPress={() => this._filePicker()} >
          <Text>file picker</Text>
        </TouchableOpacity>
      </View>
    );
  }
}
  • 操作系统:Linux Mint v18.3
  • node:v9.2.0
  • react-native-cli:2.0.1
  • react-native:0.52.0
  • api level:21
  • 虚拟avd设备Nexus_5X_API_24

0 个答案:

没有答案