fix(android): content uri handling improvements (#9936)

This commit is contained in:
Osei Fortune
2022-06-19 13:46:50 -04:00
committed by Nathan Walker
parent 464ea18737
commit 99480c06db
15 changed files with 164 additions and 92 deletions

View File

@@ -1,4 +1,4 @@
import { Page, EventData, Application, File } from '@nativescript/core';
import { Page, EventData, Application, File, Folder, knownFolders, path, getFileAccess, Utils } from '@nativescript/core';
let page: Page;
@@ -71,3 +71,108 @@ function doWork(path: string) {
console.error(e);
}
}
export function pickFiles() {
if (!global.isAndroid) {
return;
}
const Intent = android.content.Intent;
Application.android.on('activityResult', (args) => {
if (args.requestCode === 1000) {
const intent: android.content.Intent = args.intent;
const clip = intent.getClipData();
if (clip) {
const count = clip.getItemCount();
for (let i = 0; i < count; i++) {
const item = clip.getItemAt(i);
readFile(item.getUri().toString());
}
} else {
readFile(intent.getData().toString());
}
}
});
const picker = new Intent(Intent.ACTION_OPEN_DOCUMENT);
picker.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
picker.addCategory(Intent.CATEGORY_OPENABLE);
picker.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
picker.setType('*/*');
Application.android.foregroundActivity.startActivityForResult(picker, 1000);
}
export function pickFile() {
if (!global.isAndroid) {
return;
}
Application.android.on('activityResult', (args) => {
if (args.requestCode === 1000) {
const file = args.intent.getData().toString();
//const file = File.fromPath(args.intent.getData().toString());
//console.log(file);
readFile(file);
}
});
const Intent = android.content.Intent;
const picker = new Intent(Intent.ACTION_OPEN_DOCUMENT);
picker.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
picker.addCategory(Intent.CATEGORY_OPENABLE);
picker.setType('*/*');
Application.android.foregroundActivity.startActivityForResult(picker, 1000);
}
function readFile(selected: string) {
let applicationContext = Utils.android.getApplicationContext();
const getOrSetHelper: org.nativescript.widgets.FileHelper = org.nativescript.widgets.FileHelper.fromString(applicationContext, selected);
console.log('==== CONTEXT START =========');
console.log('applicationContext', applicationContext);
console.log('selected: ', decodeURIComponent(selected));
console.log('getOrSetHelper: ', getOrSetHelper);
console.log('==== CONTEXT END =========');
console.log('==== READ START =========');
const fileRead = getFileAccess().read(selected);
console.log('# read: ', fileRead);
const readSync = getFileAccess().readSync(selected);
console.log('# readSync: ', readSync);
const readText = getFileAccess().readText(selected);
console.log('# readText: ', readText);
getFileAccess()
.readAsync(selected)
.then(
(readAsyncResolve) => {
console.log('# readAsynccResolve: ', readAsyncResolve);
},
(readAsyncRejected) => {
console.log('# readAsyncRejected: ', readAsyncRejected);
}
);
console.log('==== READ END =========');
saveFile(selected, readSync);
}
function saveFile(selected, readSync) {
const folder: Folder = <Folder>knownFolders.documents();
let newPath: string = path.join(folder.path, getFileNameFromContent(selected));
File.fromPath(newPath).writeSync(readSync, (error) => {
console.log(error);
});
let savedFile = folder.getFile(getFileNameFromContent(selected));
console.log('==== SAVE START =========');
console.log('# saved file: ', savedFile);
console.log('# save size: ', savedFile.size);
console.log('==== SAVE END =========');
}
function getFileNameFromContent(content: string) {
const file = getFileAccess().getFile(content);
return decodeURIComponent(file.name).split('/').pop().toLowerCase();
}

View File

@@ -2,5 +2,7 @@
<StackLayout>
<Button text="Create Random" tap="createRandom" />
<Button text="Pick File" tap="pickFile" />
<Button text="Pick Multiple Files" tap="pickFiles" />
</StackLayout>
</Page>