我正在尝试将xlsx上传到Firestore存储,使用react-native-document-picker从
ExternalStorageDirectoryPath
中选择文件,所以当我只记录文件URI时,我不会收到错误,但一旦尝试上传文件会引发错误。
相关代码:
const uploadFile = async () => {
try {
const res = await DocumentPicker.pick({
type: [DocumentPicker.types.allFiles],
});
const task = Storage().ref('catalogue/'+ res.name).putFile(res.uri);
task.on('state_changed',
sn => {},
err => console.log(err),
() => {
console.log('excel uploaded!'+res.name)
Storage()
.ref("catalogue").child(res.name).getDownloadURL()
.then(url => {
console.log('uploaded excel url', url);
}).catch(err=>console.log(err))
}
)
await task
} catch (err) {
if (DocumentPicker.isCancel(err)) {
// User cancelled the picker, exit any dialogs or menus and move on
} else {
throw err;
}
}
}
I already included the required permissions in my
AndroidManifest.xml` 文件并重建项目
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MANAGE_DOCUMENTS"/>
但我仍然收到此错误:
权限拒绝:读取 com.android.externalStorageProvider uri content://com... 需要 android.permission.MANAGE_DOCUMENTS,或 grantUriPermission()
Here is my code that solved this issue:
handleFileSelection=async(props)=>
{
try
{
const response = await DocumentPicker.pickMultiple({
presentationStyle: 'fullScreen',
allowMultiSelection: true,
copyTo: 'cachesDirectory',
});
await this.pickImageAndUpload(response[0].fileCopyUri)
} catch (err) {console.warn(err)}
}
pickImageAndUpload = async(uri) => {
const uploadTask = storage().ref().child(`/usersChatImages/${Date.now()}`).putFile(uri)
uploadTask.on('state_changed',
(snapshot) => {
var progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
},(error) => { alert("error uploading image")},() => {
uploadTask.snapshot.ref.getDownloadURL().then((downloadURL) =>
{
alert(downloadURL)
}).catch((err)=>{alert("YESSS")}); });
}
我在最新的 Android SDK 版本的 Qt 框架中遇到了类似的问题,我通过在清单中添加 requestLegacyExternalStorage 解决了该问题(pat 应用程序):
<application android:requestLegacyExternalStorage="true" android:hardwareAccelerated="true" android:name="org.qtproject.qt5.android.bindings.QtApplication" //...
这与最近权限系统的变化有关: https://developer.android.com/about/versions/11/privacy/storage
const res = await DocumentPicker.pickSingle({
type: [DocumentPicker.types.allFiles],
copyTo: 'cachesDirectory',
});
let fileCopyUri = res.fileCopyUri
const filename = fileCopyUri.substring(fileCopyUri.lastIndexOf('/') + 1);
const task = storage().ref(filename).putFile(fileCopyUri);
// add "copyTo: 'cachesDirectory'",
// add res.fileCopyUri