|
1 |
| -import { Page, EventData, Application, File, Folder, knownFolders, path, getFileAccess, Utils } from '@nativescript/core'; |
| 1 | +import { Page, EventData, Application, File, Folder, knownFolders, path, getFileAccess, Utils, Screen, Http, AndroidDirectory, ImageSource, alert } from '@nativescript/core'; |
2 | 2 |
|
3 | 3 | let page: Page;
|
4 | 4 |
|
@@ -217,3 +217,103 @@ function getFileNameFromContent(content: string) {
|
217 | 217 | const file = getFileAccess().getFile(content);
|
218 | 218 | return decodeURIComponent(file.name).split('/').pop().toLowerCase();
|
219 | 219 | }
|
| 220 | + |
| 221 | +let lastDownload: File; |
| 222 | +export function createFileInDownloads() { |
| 223 | + if (!global.isAndroid) { |
| 224 | + return; |
| 225 | + } |
| 226 | + const width = Screen.mainScreen.widthPixels; |
| 227 | + const height = Screen.mainScreen.heightPixels; |
| 228 | + const randomImageUrl = `https://picsum.photos/${width}/${height}.jpg?random=${Date.now()}`; |
| 229 | + |
| 230 | + Http.getFile(randomImageUrl).then((result: File) => { |
| 231 | + let file = File.android.createFile({ |
| 232 | + directory: AndroidDirectory.DOWNLOADS, |
| 233 | + name: `${Date.now()}.jpg`, |
| 234 | + mime: 'image/jpeg', |
| 235 | + relativePath: `NativeScript`, |
| 236 | + }); |
| 237 | + result |
| 238 | + .copy(file.path) |
| 239 | + .then((done) => { |
| 240 | + lastDownload = file; |
| 241 | + console.log('done: ' + done + '\n' + 'Original path: ' + result.path + '\n' + 'Copied to: ' + file.path + '\n' + 'Original size: ' + result.size + '\n' + 'Copy size: ' + file.size + '\n'); |
| 242 | + alert(`File saved in ${AndroidDirectory.DOWNLOADS}/NativeScript`); |
| 243 | + }) |
| 244 | + .catch((error) => { |
| 245 | + console.error(error); |
| 246 | + }); |
| 247 | + }); |
| 248 | +} |
| 249 | + |
| 250 | +export function createFileInGallery() { |
| 251 | + if (!global.isAndroid) { |
| 252 | + return; |
| 253 | + } |
| 254 | + const width = Screen.mainScreen.widthPixels; |
| 255 | + const height = Screen.mainScreen.heightPixels; |
| 256 | + const randomImageUrl = `https://picsum.photos/${width}/${height}.jpg?random=${Date.now()}`; |
| 257 | + |
| 258 | + Http.getFile(randomImageUrl).then((result: File) => { |
| 259 | + let file = File.android.createFile({ |
| 260 | + directory: AndroidDirectory.PICTURES, |
| 261 | + name: `${Date.now()}.jpg`, |
| 262 | + mime: 'image/jpeg', |
| 263 | + relativePath: `NativeScript`, |
| 264 | + }); |
| 265 | + result |
| 266 | + .copy(file.path) |
| 267 | + .then((done) => { |
| 268 | + console.log('done: ' + done + '\n' + 'Original path: ' + result + '\n' + 'Copied to: ' + file.path + '\n' + 'Original size: ' + result.size + '\n' + 'Copy size: ' + file.size + '\n'); |
| 269 | + alert(`File saved in ${AndroidDirectory.PICTURES}/NativeScript`); |
| 270 | + }) |
| 271 | + .catch((error) => { |
| 272 | + console.error(error); |
| 273 | + }); |
| 274 | + }); |
| 275 | +} |
| 276 | + |
| 277 | +export function createFileInMusic() { |
| 278 | + if (!global.isAndroid) { |
| 279 | + return; |
| 280 | + } |
| 281 | + |
| 282 | + Http.getFile('https://github.com/rafaelreis-hotmart/Audio-Sample-files/raw/master/sample.mp3').then((result: File) => { |
| 283 | + let file = File.android.createFile({ |
| 284 | + directory: AndroidDirectory.MUSIC, |
| 285 | + name: `${Date.now()}.MP3`, |
| 286 | + mime: 'audio/mp3', |
| 287 | + relativePath: `NativeScript/MP3`, |
| 288 | + }); |
| 289 | + |
| 290 | + result |
| 291 | + .copy(file.path) |
| 292 | + .then((done) => { |
| 293 | + console.log('done: ' + done + '\n' + 'Original path: ' + result + '\n' + 'Copied to: ' + file.path + '\n' + 'Original size: ' + result.size + '\n' + 'Copy size: ' + file.size + '\n'); |
| 294 | + |
| 295 | + alert(`File saved in ${AndroidDirectory.MUSIC}/NativeScript/MP3`); |
| 296 | + }) |
| 297 | + .catch((error) => { |
| 298 | + console.error(error); |
| 299 | + }); |
| 300 | + }); |
| 301 | +} |
| 302 | + |
| 303 | +export function createAndAppendText() { |
| 304 | + const file = File.fromPath(path.join(knownFolders.temp().path, `${Date.now()}-app.txt`)); |
| 305 | + file.appendTextSync('Hello'); |
| 306 | + file.appendTextSync(' World'); |
| 307 | + const data = file.readTextSync(); |
| 308 | + console.log('createAndAppend:', data === 'Hello World'); |
| 309 | +} |
| 310 | + |
| 311 | +export function createAndAppendData() { |
| 312 | + const file = File.fromPath(path.join(knownFolders.temp().path, `${Date.now()}-app.txt`)); |
| 313 | + const hello = global.isIOS ? NSString.stringWithString('Hello').dataUsingEncoding(NSUTF8StringEncoding) : new java.lang.String('Hello').getBytes('UTF-8'); |
| 314 | + const world = global.isIOS ? NSString.stringWithString(' World').dataUsingEncoding(NSUTF8StringEncoding) : new java.lang.String(' World').getBytes('UTF-8'); |
| 315 | + file.appendSync(hello); |
| 316 | + file.appendSync(world); |
| 317 | + const data = file.readTextSync(); |
| 318 | + console.log('createAndAppendData:', data === 'Hello World'); |
| 319 | +} |
0 commit comments