如何创建文件并将其存储在下载文件夹android中? [英] How to create a file and store it inside download folder android?

查看:55
本文介绍了如何创建文件并将其存储在下载文件夹android中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 rn-fetch-blob

解决方案

为了允许访问 rn-fetch-blob 中的其他目录,如果 android 版本高于 6,则需要请求运行时权限.链接:https://facebook.github.io/react-native/docs/permissionsandroid

修改代码:

从'react-native'导入{PermissionsAndroid};saveFile = async() =>{尝试 {const 授予 = 等待 PermissionsAndroid.request(权限Android.PERMISSIONS.WRITE_EXTERNAL_STORAGE);如果(授予 === PermissionsAndroid.RESULTS.GRANTED){console.log("已授予权限");const fs = RNFetchBlob.fs;//const base64 = RNFetchBlob.base64;const dirs = RNFetchBlob.fs.dirs;控制台.log(dirs.DownloadDir);const NEW_FILE_PATH = dirs.DownloadDir + '/test.txt';fs.createFile(NEW_FILE_PATH, 'foo', 'utf8');} 别的 {console.log('权限被拒绝');}} 抓住(错误){控制台警告(错误);}}

rn-fetch-blob 的附加链接:

1) https://github.com/joltup/rn-fetch-blob/wiki/File-System-Access-API#dirs

2) https://github.com/joltup/rn-fetch-blob

I am using rn-fetch-blob https://github.com/joltup/rn-fetch-blob I am creating new file and storing it inside downloads folder. Note: I am using emulator. Now when I try to create a new file and I am using download dir see this -> https://github.com/joltup/rn-fetch-blob/wiki/File-System-Access-API#dirs but I am getting error saying permission denied why so ? How can I enable permission so that file gets stored inside download folder in android react native app ?

Code:

saveFile = () => {
        const fs = RNFetchBlob.fs;

        const dirs = RNFetchBlob.fs.dirs;
        console.log("directory path: ",dirs.DownloadDir );

        const NEW_FILE_PATH = dirs.DownloadDir + '/demo.txt';

        console.log("file path: ", NEW_FILE_PATH);

        fs.createFile(NEW_FILE_PATH, 'foo', 'utf8');

    }

AndroidManifest.xml:

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission tools:node="remove" android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission tools:node="remove" android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission tools:node="remove" android:name="android.permission.READ_EXTERNAL_STORAGE" />

Screenshot:

解决方案

For allowing acccess to other dirs in rn-fetch-blob if android version is above 6 then you need to ask for runtime permission. Link: https://facebook.github.io/react-native/docs/permissionsandroid

Modified code:

import {PermissionsAndroid} from 'react-native';

    saveFile = async () => {

            try {
                const granted = await PermissionsAndroid.request(
                    PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE
                );
                if (granted === PermissionsAndroid.RESULTS.GRANTED) {
                    console.log("Permission granted");

                    const fs = RNFetchBlob.fs;
                    // const base64 = RNFetchBlob.base64;

                    const dirs = RNFetchBlob.fs.dirs;
                    console.log(dirs.DownloadDir);

                    const NEW_FILE_PATH = dirs.DownloadDir + '/test.txt';
                    fs.createFile(NEW_FILE_PATH, 'foo', 'utf8');

                } else {
                    console.log('Permission denied');
                }
                } catch (err) {
                    console.warn(err);
                }

        }

Additional links for rn-fetch-blob:

1) https://github.com/joltup/rn-fetch-blob/wiki/File-System-Access-API#dirs

2) https://github.com/joltup/rn-fetch-blob

这篇关于如何创建文件并将其存储在下载文件夹android中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆