Skip to content

Commit 8e7bfe9

Browse files
committed
chore: updated example app to ask storage permission in android
1 parent f1991ce commit 8e7bfe9

1 file changed

Lines changed: 44 additions & 1 deletion

File tree

example/src/App.tsx

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,57 @@
11
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';
310
import * as RNFS from '@dr.pogodin/react-native-fs';
411
import TinyWavPackDecoder from 'react-native-tiny-wavpack-decoder';
512

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+
645
export default function App() {
746
const [result, setResult] = useState<string | null>(null);
847
const [error, setError] = useState<string | null>(null);
948
const [progress, setProgress] = useState(0);
1049

1150
useEffect(() => {
51+
if (Platform.OS === 'android') {
52+
requestStoragePermission();
53+
}
54+
1255
console.log('Setting up progress listener');
1356
const subscription = TinyWavPackDecoder.addProgressListener((p: number) => {
1457
console.log(`Progress update: ${p}`);

0 commit comments

Comments
 (0)