|
| 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 | +} |
0 commit comments