[英]Save Files in the Android data folder
import RNFetchBlob from 'rn-fetch-blob'
var path = RNFetchBlob.fs.dirs.DocumentDir
RNFetchBlob
.config({ path: toFile })
.fetch('GET', fromUrl)
.then(res => {
})
});
使用上面的代码在 React native 中下载文件并在 ios 中运行良好。但我在 android 中执行此操作,它直接显示正在下载但未找到文件。 我想将文件保存在 (Android=>data=>com.appname) 文件夹中。 但是这段代码无法下载它。
我尝试了 DownloadDir,它正在工作并将文件保存在下载文件夹中。 但我想将它保存在 (Android=>data=>com.appname) 文件夹中。 所以无法理解它将如何工作。
此示例将文件保存在 MyApp 文件夹下的以下路径
/data/user/0/com.filesystem/files/MyApp/
由于 android 最新的隐私政策(我认为来自 Android 10 +),它没有通过外部文件管理器显示内容,因为该外部文件管理器应用程序必须获得额外的许可。
您可以使用下面的文件管理器查看数据文件夹中的文件https://play.google.com/store/apps/details?id=com.alphainventor.filemanager
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.filesystem">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
<uses-permission android:name="android.permission.MANAGE_DOCUMENTS"/>
<application
android:name=".MainApplication"
android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher_round"
android:allowBackup="false"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize|uiMode"
android:launchMode="singleTask"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.DOWNLOAD_COMPLETE"/>
</intent-filter>
</activity>
</application>
</manifest>
应用程序.js
import React, { useEffect, useState } from 'react'
import { SafeAreaView, View, Text, TouchableOpacity, FlatList, Image, Dimensions } from 'react-native'
import RNFetchBlob from 'rn-fetch-blob'
const { height, width } = Dimensions.get('window')
export default function App() {
const [files, setFiles] = useState([])
useEffect(() => {
handleGetFileList()
}, [])
async function handleGetFileList() {
const path = RNFetchBlob.fs.dirs.DocumentDir + '/' + 'MyApp'
await RNFetchBlob.fs.isDir(path).then(isDir => {
console.log('isDir', isDir)
if (isDir == true) {
RNFetchBlob.fs.lstat(path).then(filesList => {
console.log('filesList', filesList)
setFiles(filesList)
})
.catch(e => {
console.log('Unable to get files list', e)
})
}
})
.catch(e => {
console.log('Error isDir', e)
})
}
function handleDownloadFile() {
console.log('Hiii')
const destinationPath = RNFetchBlob.fs.dirs.DocumentDir + '/' + 'MyApp'
const url = 'https://shotkit.com/wp-content/uploads/2021/06/cool-profile-pic-matheus-ferrero.jpeg'
const fileName = Date.now()
const fileExtention = url.split('.').pop();
const fileFullName = fileName + '.' + fileExtention
console.log('fileName', fileName)
console.log('fileExtention', fileName)
console.log('fileName', fileFullName)
RNFetchBlob
.config({ path: destinationPath + '/' + fileFullName, fileCache: true })
.fetch('GET', url)
.then((res) => {
console.log('The file saved to ', res.path())
handleGetFileList()
})
}
function handleDeleteFiles() {
const path = RNFetchBlob.fs.dirs.DocumentDir + '/' + 'MyApp'
RNFetchBlob.fs.unlink(path)
.then(() => {
setFiles([])
})
.catch((err) => { })
}
function renderItem({ item, index }) {
return (
<Image
source={{ uri: 'file://' + item.path }}
style={{ height: 150, width: width / 4, borderRadius: 10, borderWidth: 1, borderColor: 'black', margin: 10 }}
resizeMode='cover'
/>
)
}
return (
<SafeAreaView style={{ flex: 1, alignItems: 'center', justifyContent: 'center', }}>
<View style={{ flex: 4, alignItems: 'center', justifyContent: 'space-around', }}>
<TouchableOpacity
onPress={handleGetFileList}
style={{ height: 45, width: 150, borderRadius: 10, borderWidth: 1, borderColor: 'black', alignItems: 'center', justifyContent: 'center', }}>
<Text>
Get the files
</Text>
</TouchableOpacity>
<TouchableOpacity
onPress={handleDownloadFile}
style={{ height: 45, width: 150, borderRadius: 10, borderWidth: 1, borderColor: 'black', alignItems: 'center', justifyContent: 'center', }}>
<Text>
Download the files
</Text>
</TouchableOpacity>
<TouchableOpacity
onPress={handleDeleteFiles}
style={{ height: 45, width: 150, borderRadius: 10, borderWidth: 1, borderColor: 'black', alignItems: 'center', justifyContent: 'center', }}>
<Text>
Delete all files
</Text>
</TouchableOpacity>
{/* <View style={{ height: '100%', width: 10 }} /> */}
</View>
<View style={{ flex: 6 }}>
<FlatList
data={files}
keyExtractor={(item, index) => String(index)}
renderItem={renderItem}
numColumns={3}
/>
</View>
</SafeAreaView>
)
}
预习
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.