FIX: open & save files to file system

This commit is contained in:
Overtorment 2024-01-16 19:51:48 +00:00
parent 146138cba5
commit 92ea58dc85
3 changed files with 20 additions and 7 deletions

View file

@ -68,7 +68,7 @@
<activity <activity
android:name=".MainActivity" android:name=".MainActivity"
android:label="@string/app_name" android:label="@string/app_name"
android:launchMode="singleInstance" android:launchMode="singleTop"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize|uiMode" android:configChanges="keyboard|keyboardHidden|orientation|screenSize|uiMode"
android:windowSoftInputMode="adjustResize" android:windowSoftInputMode="adjustResize"
android:exported="true"> android:exported="true">

View file

@ -10,7 +10,7 @@ import alert from '../components/Alert';
import { readFile } from './react-native-bw-file-access'; import { readFile } from './react-native-bw-file-access';
const LocalQRCode = require('@remobile/react-native-qrcode-local-image'); const LocalQRCode = require('@remobile/react-native-qrcode-local-image');
const writeFileAndExportToAndroidDestionation = async ({ filename, contents, destinationLocalizedString, destination }) => { const _writeFileAndExportToAndroidDestionation = async ({ filename, contents, destinationLocalizedString, destination }) => {
const granted = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE, { const granted = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE, {
title: loc.send.permission_storage_title, title: loc.send.permission_storage_title,
message: loc.send.permission_storage_message, message: loc.send.permission_storage_message,
@ -18,11 +18,24 @@ const writeFileAndExportToAndroidDestionation = async ({ filename, contents, des
buttonNegative: loc._.cancel, buttonNegative: loc._.cancel,
buttonPositive: loc._.ok, buttonPositive: loc._.ok,
}); });
// In Android 13 no WRITE_EXTERNAL_STORAGE permission is needed
// @see https://stackoverflow.com/questions/76311685/permissionandroid-request-always-returns-never-ask-again-without-any-prompt-r
if (granted === PermissionsAndroid.RESULTS.GRANTED || Platform.Version >= 33) { if (granted === PermissionsAndroid.RESULTS.GRANTED || Platform.Version >= 33) {
const filePath = destination + `/${filename}`; const filePath = destination + `/${filename}`;
try { try {
await RNFS.writeFile(filePath, contents); await RNFS.writeFile(filePath, contents);
alert(loc.formatString(loc._.file_saved, { filePath: filename, destination: destinationLocalizedString })); console.log(`file saved to ${filePath}`);
await Share.open({
url: 'file://' + filePath,
saveToFiles: isDesktop,
})
.catch(error => {
console.log(error);
})
.finally(() => {
RNFS.unlink(filePath);
});
} catch (e) { } catch (e) {
console.log(e); console.log(e);
alert(e.message); alert(e.message);
@ -57,11 +70,11 @@ const writeFileAndExport = async function (filename, contents) {
RNFS.unlink(filePath); RNFS.unlink(filePath);
}); });
} else if (Platform.OS === 'android') { } else if (Platform.OS === 'android') {
await writeFileAndExportToAndroidDestionation({ await _writeFileAndExportToAndroidDestionation({
filename, filename,
contents, contents,
destinationLocalizedString: loc._.downloads_folder, destinationLocalizedString: loc._.downloads_folder,
destination: RNFS.DownloadDirectoryPath, destination: RNFS.DocumentDirectoryPath,
}); });
} }
}; };

View file

@ -42,7 +42,7 @@ export default class Selftest extends Component {
} }
onPressSaveToStorage = () => { onPressSaveToStorage = () => {
fs.writeFileAndExport('bluewallet-storagesave-test.txt', 'Success'); fs.writeFileAndExport('bluewallet-storagesave-test.txt', 'Success on ' + new Date().toUTCString());
}; };
onPressImportDocument = async () => { onPressImportDocument = async () => {
@ -318,7 +318,7 @@ export default class Selftest extends Component {
<BlueSpacing20 /> <BlueSpacing20 />
<Button title="Test Save to Storage" onPress={this.onPressSaveToStorage} /> <Button title="Test Save to Storage" onPress={this.onPressSaveToStorage} />
<BlueSpacing20 /> <BlueSpacing20 />
<Button title="Test Save to File Import" onPress={this.onPressImportDocument} /> <Button title="Test File Import" onPress={this.onPressImportDocument} />
</ScrollView> </ScrollView>
</BlueCard> </BlueCard>
</SafeArea> </SafeArea>