MCPcopy Index your code
hub / github.com/NativeScript/NativeScript / createFile

Method createFile

packages/core/file-system/index.ts:268–297  ·  view source on GitHub ↗
(options: { relativePath?: string; name: string; mime: string; directory: AndroidDirectory })

Source from the content-addressed store, hash-verified

266
267class Android {
268 createFile(options: { relativePath?: string; name: string; mime: string; directory: AndroidDirectory }): File {
269 if (!__ANDROID__) {
270 throw new Error(`createFile is available on Android only!`);
271 }
272
273 const context = getNativeApp<android.app.Application>().getApplicationContext() as android.content.Context;
274
275 const meta = new android.content.ContentValues();
276 meta.put(android.provider.MediaStore.MediaColumns.DISPLAY_NAME, options.name);
277 meta.put(android.provider.MediaStore.MediaColumns.MIME_TYPE, options.mime);
278 //meta.put(android.provider.MediaStore.MediaColumns.DATE_ADDED, java.lang.System.currentTimeMillis() as any);
279
280 const externalDirectory = getAndroidDirectory(options.directory);
281
282 if (SDK_VERSION >= 29) {
283 const relativePath = options?.relativePath ? `/${options.relativePath}` : '';
284 meta.put(android.provider.MediaStore.MediaColumns.RELATIVE_PATH, `${externalDirectory.path}${relativePath}`);
285 // todo
286 // meta.put(android.provider.MediaStore.MediaColumns.IS_PENDING, java.lang.Integer.valueOf(1));
287 } else {
288 const relativePath = options?.relativePath ? `${options.relativePath}/` : '';
289 const directory = android.os.Environment.getExternalStoragePublicDirectory(externalDirectory.path);
290 const file = new java.io.File(directory, `${relativePath}${options.name}`);
291 meta.put(android.provider.MediaStore.MediaColumns.DATA, file.getAbsolutePath());
292 }
293
294 const uri = context.getContentResolver().insert(externalDirectory.column, meta);
295
296 return File.fromPath(uri.toString());
297 }
298}
299
300const ad = new Android();

Callers 4

createFileInDownloadsFunction · 0.80
createFileInGalleryFunction · 0.80
createFileInMusicFunction · 0.80
testFuncFunction · 0.80

Calls 4

getNativeAppFunction · 0.90
getAndroidDirectoryFunction · 0.85
fromPathMethod · 0.45
toStringMethod · 0.45

Tested by

no test coverage detected