mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
Renamed image-impl to image-native. Renamed console-helper to console-native.
This commit is contained in:
42
image/image-native.android.ts
Normal file
42
image/image-native.android.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import appModule = require("application/application");
|
||||
|
||||
export var fromResource = function (name: string) {
|
||||
var androidApp = appModule.android;
|
||||
var res = androidApp.context.getResources();
|
||||
if (res) {
|
||||
var identifier: number = res.getIdentifier(name, 'drawable', androidApp.packageName);
|
||||
if (0 < identifier) {
|
||||
return android.graphics.BitmapFactory.decodeResource(res, identifier);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
export var fromFile = function (path: string) {
|
||||
return android.graphics.BitmapFactory.decodeFile(path, null);
|
||||
}
|
||||
|
||||
export var fromData = function (data: any) {
|
||||
return android.graphics.BitmapFactory.decodeStream(data);
|
||||
}
|
||||
|
||||
export var saveToFile = function (instance: android.graphics.Bitmap, path: string, format: number, quality = 100): boolean {
|
||||
if (!instance) {
|
||||
return false;
|
||||
}
|
||||
|
||||
var targetFormat = android.graphics.Bitmap.CompressFormat.PNG;
|
||||
switch (format) {
|
||||
case 1: // JPEG
|
||||
targetFormat = android.graphics.Bitmap.CompressFormat.JPEG;
|
||||
break;
|
||||
}
|
||||
|
||||
// TODO add exception handling
|
||||
var outputStream = new java.io.BufferedOutputStream(new java.io.FileOutputStream(path));
|
||||
|
||||
var res = instance.compress(targetFormat, quality, outputStream);
|
||||
outputStream.close();
|
||||
return res;
|
||||
}
|
||||
Reference in New Issue
Block a user