8000 feat(android): content uri support for File (#9807) · NativeScript/NativeScript@c68d002 · GitHub
[go: up one dir, main page]

Skip to content

Commit c68d002

Browse files
authored
feat(android): content uri support for File (#9807)
1 parent d2f166b commit c68d002

File tree

9 files changed

+931
-22
lines changed

9 files changed

+931
-22
lines changed

apps/toolbox/src/main-page.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<Button text="touch-animations" tap="{{ viewDemo }}" class="btn btn-primary btn-view-demo" />
1818
<Button text="vector-image" tap="{{ viewDemo }}" class="btn btn-primary btn-view-demo" />
1919
<Button text="visibility-vs-hidden" tap="{{ viewDemo }}" class="btn btn-primary btn-view-demo" />
20+
<Button text="fs-helper" tap="{{ viewDemo }}" class="btn btn-primary btn-view-demo" />
2021
</StackLayout>
2122
</ScrollView>
2223
</StackLayout>

apps/toolbox/src/pages/fs-helper.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import { Page, EventData, Application, File } from '@nativescript/core';
2+
3+
let page: Page;
4+
5+
export function navigatingTo(args: EventData) {
6+
page = <Page>args.object;
7+
}
8+
9+
export function createRandom(args) {
10+
if (global.isAndroid) {
11+
try {
12+
const activity = Application.android.foregroundActivity as androidx.appcompat.app.AppCompatActivity;
13+
const selection = [android.provider.MediaStore.MediaColumns.DISPLAY_NAME, android.provider.MediaStore.MediaColumns._ID];
14+
// testing with downloads as rename only works with a well know collection downloads/audio/photos/videos API 29+
15+
let cursor = activity.getContentResolver().query(android.provider.MediaStore.Downloads.getContentUri('external'), selection, null, null);
16+
17+
let uri;
18+
19+
while (cursor.moveToNext()) {
20+
const index = cursor.getColumnIndex(selection[0]);
21+
const name = cursor.getString(index 10000 );
22+
if (name === 'ns_tmp.txt') {
23+
const idIndex = cursor.getColumnIndex(selection[1]);
24+
const id = cursor.getLong(idIndex);
25+
uri = android.net.Uri.parse(`${android.provider.MediaStore.Downloads.getContentUri('external').toString()}/${id}`);
26+
cursor.close();
27+
break;
28+
}
29+
}
30+
31+
if (!uri) {
32+
const values = new android.content.ContentValues();
33+
values.put(android.provider.MediaStore.MediaColumns.DISPLAY_NAME, 'ns_tmp.txt');
34+
values.put(android.provider.MediaStore.MediaColumns.MIME_TYPE, 'text/plain');
35+
uri = activity.getContentResolver().insert(android.provider.MediaStore.Downloads.getContentUri('external'), values);
36+
}
37+
38+
doWork(uri.toString());
39+
} catch (e) {
40+
console.error(e);
41+
}
42+
}
43+
}
44+
45+
function doWork(path: string) {
46+
try {
47+
const file = File.fromPath(path) as File;
48+
console.log('name: ', file.name);
49+
console.log('path: ', file.path);
50+
console.log('parent: ', file.parent);
51+
console.log('size: ', file.size);
52+
console.log('lastModified: ', file.lastModified);
53+
console.log('extension: ', file.extension);
54+
if (file.size > 0) {
55+
console.log('current text: ', file.readTextSync());
56+
} else {
57+
file.writeTextSync('Hello World');
58+
console.log('after write: ', file.readTextSync());
59+
console.log('after write size: ', file.size);
60+
}
61+
62+
file.renameSync(`ns_temp_${Date.now()}.txt`);
63+
64+
console.log('rename: ', file.name);
65+
console.log('rename lastModified: ', file.lastModified);
66+
67+
file.removeSync();
68+
69+
console.log('deleted ?', !File.exists(path));
70+
} catch (e) {
71+
console.error(e);
72+
}
73+
}

apps/toolbox/src/pages/fs-helper.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<Page xmlns="http://schemas.nativescript.org/tns.xsd" navigatingTo="navigatingTo" class="page">
2+
3+
<StackLayout>
4+
<Button text="Create Random" tap="createRandom" />
5+
</StackLayout>
6+
</Page>

0 commit comments

Comments
 (0)
0