1 |
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
import React, {Component} from 'react'; import { View, Text, Button, Alert, ToastAndroid, PermissionsAndroid, ActivityIndicator, } from 'react-native'; import RNFetchBlob from 'rn-fetch-blob'; export default class App extends Component { constructor(props) { super(props); this.state = { loading: false, }; } actualDownload = () => { this.setState({ loading: true, }); let dirs = RNFetchBlob.fs.dirs; RNFetchBlob.config({ // add this option that makes response data to be stored as a file, // this is much more performant. path: dirs.DownloadDir + '/path-to-file.png', fileCache: true, }) .fetch( 'GET', 'https://cdn.pixabay.com/photo/2014/12/21/23/34/cherry-575547_960_720.png', {}, ) .then((res) => { this.setState({ loading: false, }); ToastAndroid.showWithGravity( 'Your file has been downloaded to downloads folder!', ToastAndroid.SHORT, ToastAndroid.BOTTOM, ); }); }; async downloadFile() { try { const granted = await PermissionsAndroid.request( PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE, { title: 'Storage Permission', message: 'App needs access to memory to download the file ', }, ); if (granted === PermissionsAndroid.RESULTS.GRANTED) { this.actualDownload(); } else { Alert.alert( 'Permission Denied!', 'You need to give storage permission to download the file', ); } } catch (err) { console.log(err); } } render() { return ( <View> <Text> Download Files in Android </Text> <Button onPress={() => this.downloadFile()} title="Download" /> {this.state.loading ? ( <ActivityIndicator size="small" color="#0000ff" /> ) : null} </View> ); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
import React, {useState} from 'react'; import { View, Text, PermissionsAndroid, Button, ActivityIndicator, ToastAndroid, Alert, } from 'react-native'; import RNFetchBlob from 'rn-fetch-blob'; const App = () => { const [loading, setLoading] = useState(false); const actualDownload = () => { setLoading(true); let dirs = RNFetchBlob.fs.dirs; RNFetchBlob.config({ // add this option that makes response data to be stored as a file, // this is much more performant. path: dirs.DownloadDir + '/path-to-file.png', fileCache: true, }) .fetch( 'GET', 'https://cdn.pixabay.com/photo/2014/12/21/23/34/cherry-575547_960_720.png', {}, ) .then((res) => { setLoading(false); ToastAndroid.showWithGravity( 'Your file has been downloaded to downloads folder!', ToastAndroid.SHORT, ToastAndroid.BOTTOM, ); }); }; const downloadFile = async () => { try { const granted = await PermissionsAndroid.request( PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE, { title: 'Storage Permission', message: 'App needs access to memory to download the file ', }, ); if (granted === PermissionsAndroid.RESULTS.GRANTED) { actualDownload(); } else { Alert.alert( 'Permission Denied!', 'You need to give storage permission to download the file', ); } } catch (err) { console.log(err); } }; return ( <View> <Text> Download Files in Android </Text> <Button onPress={() => downloadFile()} title="Download" /> {loading ? <ActivityIndicator size="small" color="#0000ff" /> : null} </View> ); }; export default App; |