我在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>
);
}
}