mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-17 21:01:34 +08:00
Merge pull request #2069 from triniwiz/master
Added save to gallery option - Android
This commit is contained in:
@ -1,6 +1,5 @@
|
|||||||
import * as typesModule from "utils/types";
|
import * as typesModule from "utils/types";
|
||||||
import * as utilsModule from "utils/utils";
|
import * as utilsModule from "utils/utils";
|
||||||
import * as fileSystemModule from "file-system";
|
|
||||||
import * as applicationModule from "application";
|
import * as applicationModule from "application";
|
||||||
import * as imageSourceModule from "image-source";
|
import * as imageSourceModule from "image-source";
|
||||||
import * as cameraCommonModule from "./camera-common";
|
import * as cameraCommonModule from "./camera-common";
|
||||||
@ -15,6 +14,7 @@ export var takePicture = function (options?): Promise<any> {
|
|||||||
|
|
||||||
var density = utils.layout.getDisplayDensity();
|
var density = utils.layout.getDisplayDensity();
|
||||||
if (options) {
|
if (options) {
|
||||||
|
var saveToGallery = options.saveToGallery ? true : false;
|
||||||
var reqWidth = options.width ? options.width * density : 0;
|
var reqWidth = options.width ? options.width * density : 0;
|
||||||
var reqHeight = options.height ? options.height * density : reqWidth;
|
var reqHeight = options.height ? options.height * density : reqWidth;
|
||||||
var shouldKeepAspectRatio = types.isNullOrUndefined(options.keepAspectRatio) ? true : options.keepAspectRatio;
|
var shouldKeepAspectRatio = types.isNullOrUndefined(options.keepAspectRatio) ? true : options.keepAspectRatio;
|
||||||
@ -22,12 +22,19 @@ export var takePicture = function (options?): Promise<any> {
|
|||||||
var takePictureIntent = new android.content.Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
|
var takePictureIntent = new android.content.Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
|
||||||
var dateStamp = createDateTimeStamp();
|
var dateStamp = createDateTimeStamp();
|
||||||
|
|
||||||
var fileSystem: typeof fileSystemModule = require("file-system");
|
if (saveToGallery) {
|
||||||
|
var picturePath = android.os.Environment.getExternalStoragePublicDirectory(
|
||||||
var tempPicturePath = fileSystem.path.join(utils.ad.getApplicationContext().getExternalFilesDir(null).getAbsolutePath(), "cameraPicture_" + dateStamp + ".jpg");
|
android.os.Environment.DIRECTORY_PICTURES).getAbsolutePath() + "/" + "cameraPicture_" + dateStamp + ".jpg";
|
||||||
var nativeFile = new java.io.File(tempPicturePath);
|
var nativeFile = new java.io.File(picturePath);
|
||||||
var tempPictureUri = android.net.Uri.fromFile(nativeFile);
|
var tempPictureUri = android.net.Uri.fromFile(nativeFile);
|
||||||
takePictureIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, tempPictureUri);
|
takePictureIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, tempPictureUri);
|
||||||
|
} else {
|
||||||
|
var picturePath = utils.ad.getApplicationContext().getExternalFilesDir(null).getAbsolutePath() + "/" + "cameraPicture_" + dateStamp + ".jpg";
|
||||||
|
var nativeFile = new java.io.File(picturePath);
|
||||||
|
var tempPictureUri = android.net.Uri.fromFile(nativeFile);
|
||||||
|
takePictureIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, tempPictureUri);
|
||||||
|
}
|
||||||
|
|
||||||
if (takePictureIntent.resolveActivity(utils.ad.getApplicationContext().getPackageManager()) != null) {
|
if (takePictureIntent.resolveActivity(utils.ad.getApplicationContext().getPackageManager()) != null) {
|
||||||
|
|
||||||
var appModule: typeof applicationModule = require("application");
|
var appModule: typeof applicationModule = require("application");
|
||||||
@ -37,15 +44,19 @@ export var takePicture = function (options?): Promise<any> {
|
|||||||
appModule.android.onActivityResult = previousResult;
|
appModule.android.onActivityResult = previousResult;
|
||||||
|
|
||||||
if (requestCode === REQUEST_IMAGE_CAPTURE && resultCode === android.app.Activity.RESULT_OK) {
|
if (requestCode === REQUEST_IMAGE_CAPTURE && resultCode === android.app.Activity.RESULT_OK) {
|
||||||
|
var imageSource: typeof imageSourceModule = require("image-source");
|
||||||
|
if (saveToGallery) {
|
||||||
|
resolve({image:imageSource.fromFile(picturePath) ,path:picturePath});
|
||||||
|
} else {
|
||||||
var options = new android.graphics.BitmapFactory.Options();
|
var options = new android.graphics.BitmapFactory.Options();
|
||||||
options.inJustDecodeBounds = true;
|
options.inJustDecodeBounds = true;
|
||||||
android.graphics.BitmapFactory.decodeFile(tempPicturePath, options);
|
android.graphics.BitmapFactory.decodeFile(picturePath, options);
|
||||||
|
|
||||||
var sampleSize = calculateInSampleSize(options.outWidth, options.outHeight, reqWidth, reqHeight);
|
var sampleSize = calculateInSampleSize(options.outWidth, options.outHeight, reqWidth, reqHeight);
|
||||||
|
|
||||||
var finalBitmapOptions = new android.graphics.BitmapFactory.Options();
|
var finalBitmapOptions = new android.graphics.BitmapFactory.Options();
|
||||||
finalBitmapOptions.inSampleSize = sampleSize;
|
finalBitmapOptions.inSampleSize = sampleSize;
|
||||||
var bitmap = android.graphics.BitmapFactory.decodeFile(tempPicturePath, finalBitmapOptions);
|
var bitmap = android.graphics.BitmapFactory.decodeFile(picturePath, finalBitmapOptions);
|
||||||
var scaledSizeImage = null;
|
var scaledSizeImage = null;
|
||||||
if (reqHeight > 0 && reqWidth > 0) {
|
if (reqHeight > 0 && reqWidth > 0) {
|
||||||
if (shouldKeepAspectRatio) {
|
if (shouldKeepAspectRatio) {
|
||||||
@ -63,7 +74,7 @@ export var takePicture = function (options?): Promise<any> {
|
|||||||
scaledSizeImage = bitmap;
|
scaledSizeImage = bitmap;
|
||||||
}
|
}
|
||||||
|
|
||||||
var ei = new android.media.ExifInterface(tempPicturePath);
|
var ei = new android.media.ExifInterface(picturePath);
|
||||||
var orientation = ei.getAttributeInt(android.media.ExifInterface.TAG_ORIENTATION, android.media.ExifInterface.ORIENTATION_NORMAL);
|
var orientation = ei.getAttributeInt(android.media.ExifInterface.TAG_ORIENTATION, android.media.ExifInterface.ORIENTATION_NORMAL);
|
||||||
|
|
||||||
switch (orientation) {
|
switch (orientation) {
|
||||||
@ -78,9 +89,9 @@ export var takePicture = function (options?): Promise<any> {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
var imageSource: typeof imageSourceModule = require("image-source");
|
resolve({image:imageSource.fromNativeSource(scaledSizeImage), path:picturePath});
|
||||||
|
|
||||||
resolve(imageSource.fromNativeSource(scaledSizeImage));
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user