Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat(android):add content uri support for File #9807

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/toolbox/src/main-page.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<Button text="touch-animations" tap="{{ viewDemo }}" class="btn btn-primary btn-view-demo" />
<Button text="vector-image" tap="{{ viewDemo }}" class="btn btn-primary btn-view-demo" />
<Button text="visibility-vs-hidden" tap="{{ viewDemo }}" class="btn btn-primary btn-view-demo" />
<Button text="fs-helper" tap="{{ viewDemo }}" class="btn btn-primary btn-view-demo" />
</StackLayout>
</ScrollView>
</StackLayout>
Expand Down
73 changes: 73 additions & 0 deletions apps/toolbox/src/pages/fs-helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { Page, EventData, Application, File } from '@nativescript/core';

let page: Page;

export function navigatingTo(args: EventData) {
page = <Page>args.object;
}

export function createRandom(args) {
if (global.isAndroid) {
try {
const activity = Application.android.foregroundActivity as androidx.appcompat.app.AppCompatActivity;
const selection = [android.provider.MediaStore.MediaColumns.DISPLAY_NAME, android.provider.MediaStore.MediaColumns._ID];
// testing with downloads as rename only works with a well know collection downloads/audio/photos/videos API 29+
let cursor = activity.getContentResolver().query(android.provider.MediaStore.Downloads.getContentUri('external'), selection, null, null);

let uri;

while (cursor.moveToNext()) {
const index = cursor.getColumnIndex(selection[0]);
const name = cursor.getString(index);
if (name === 'ns_tmp.txt') {
const idIndex = cursor.getColumnIndex(selection[1]);
const id = cursor.getLong(idIndex);
uri = android.net.Uri.parse(`${android.provider.MediaStore.Downloads.getContentUri('external').toString()}/${id}`);
cursor.close();
break;
}
}

if (!uri) {
const values = new android.content.ContentValues();
values.put(android.provider.MediaStore.MediaColumns.DISPLAY_NAME, 'ns_tmp.txt');
values.put(android.provider.MediaStore.MediaColumns.MIME_TYPE, 'text/plain');
uri = activity.getContentResolver().insert(android.provider.MediaStore.Downloads.getContentUri('external'), values);
}

doWork(uri.toString());
} catch (e) {
console.error(e);
}
}
}

function doWork(path: string) {
try {
const file = File.fromPath(path) as File;
console.log('name: ', file.name);
console.log('path: ', file.path);
console.log('parent: ', file.parent);
console.log('size: ', file.size);
console.log('lastModified: ', file.lastModified);
console.log('extension: ', file.extension);
if (file.size > 0) {
console.log('current text: ', file.readTextSync());
} else {
file.writeTextSync('Hello World');
console.log('after write: ', file.readTextSync());
console.log('after write size: ', file.size);
}

file.renameSync(`ns_temp_${Date.now()}.txt`);

console.log('rename: ', file.name);
console.log('rename lastModified: ', file.lastModified);

file.removeSync();

console.log('deleted ?', !File.exists(path));
} catch (e) {
console.error(e);
}
}
6 changes: 6 additions & 0 deletions apps/toolbox/src/pages/fs-helper.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<Page xmlns="http://schemas.nativescript.org/tns.xsd" navigatingTo="navigatingTo" class="page">

<StackLayout>
<Button text="Create Random" tap="createRandom" />
</StackLayout>
</Page>
Loading