|
1 | 1 | import { useEffect, useState } from 'react'; |
2 | | -import { Button, StyleSheet, Text, View } from 'react-native'; |
| 2 | +import { |
| 3 | + Button, |
| 4 | + PermissionsAndroid, |
| 5 | + Platform, |
| 6 | + StyleSheet, |
| 7 | + Text, |
| 8 | + View, |
| 9 | +} from 'react-native'; |
3 | 10 | import * as RNFS from '@dr.pogodin/react-native-fs'; |
4 | 11 | import TinyWavPackDecoder from 'react-native-tiny-wavpack-decoder'; |
5 | 12 |
|
| 13 | +const sourcePath = '/sdcard/sample.wv'; |
| 14 | +const destinationPath = `${RNFS.DocumentDirectoryPath}/sample.wv`; |
| 15 | + |
| 16 | +const copyFile = async () => { |
| 17 | + RNFS.copyFile(sourcePath, destinationPath) |
| 18 | + .then(() => console.log('File copied to DocumentDirectoryPath')) |
| 19 | + .catch((err) => console.error('Error copying file:', err)); |
| 20 | +}; |
| 21 | + |
| 22 | +const requestStoragePermission = async () => { |
| 23 | + try { |
| 24 | + const granted = await PermissionsAndroid.request( |
| 25 | + PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE, |
| 26 | + { |
| 27 | + title: 'Storage Permission', |
| 28 | + message: 'This app needs access to your storage to read files.', |
| 29 | + buttonNeutral: 'Ask Me Later', |
| 30 | + buttonNegative: 'Cancel', |
| 31 | + buttonPositive: 'OK', |
| 32 | + } |
| 33 | + ); |
| 34 | + if (granted === PermissionsAndroid.RESULTS.GRANTED) { |
| 35 | + console.log('Storage permission granted'); |
| 36 | + copyFile(); |
| 37 | + } else { |
| 38 | + console.log('Storage permission denied'); |
| 39 | + } |
| 40 | + } catch (err) { |
| 41 | + console.warn(err); |
| 42 | + } |
| 43 | +}; |
| 44 | + |
6 | 45 | export default function App() { |
7 | 46 | const [result, setResult] = useState<string | null>(null); |
8 | 47 | const [error, setError] = useState<string | null>(null); |
9 | 48 | const [progress, setProgress] = useState(0); |
10 | 49 |
|
11 | 50 | useEffect(() => { |
| 51 | + if (Platform.OS === 'android') { |
| 52 | + requestStoragePermission(); |
| 53 | + } |
| 54 | + |
12 | 55 | console.log('Setting up progress listener'); |
13 | 56 | const subscription = TinyWavPackDecoder.addProgressListener((p: number) => { |
14 | 57 | console.log(`Progress update: ${p}`); |
|
0 commit comments