8000 Merge pull request #2069 from triniwiz/master · hbcodeXCI/NativeScript@3ee3f9c · GitHub
[go: up one dir, main page]

Skip to content

Commit 3ee3f9c

Browse files
committed
Merge pull request NativeScript#2069 from triniwiz/master
Added save to gallery option - Android
2 parents de748d4 + fb19294 commit 3ee3f9c

File tree

1 file changed

+57
-46
lines changed

1 file changed

+57
-46
lines changed

camera/camera.android.ts

Lines changed: 57 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import * as typesModule from "utils/types";
1+
import * as typesModule from "utils/types";
22
import * as utilsModule from "utils/utils";
3-
import * as fileSystemModule from "file-system";
43
import * as applicationModule from "application";
54
import * as imageSourceModule from "image-source";
65
import * as cameraCommonModule from "./camera-common";
@@ -15,19 +14,27 @@ export var takePicture = function (options?): Promise<any> {
1514

1615
var density = utils.layout.getDisplayDensity();
1716
if (options) {
17+
var saveToGallery = options.saveToGallery ? true : false;
1818
var reqWidth = options.width ? options.width * density : 0;
1919
var reqHeight = options.height ? options.height * density : reqWidth;
2020
var shouldKeepAspectRatio = types.isNullOrUndefined(options.keepAspectRatio) ? true : options.keepAspectRatio;
2121
}
2222
var takePictureIntent = new android.content.Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
2323
var dateStamp = createDateTimeStamp();
2424

25-
var fileSystem: typeof fileSystemModule = require("file-system");
25+
if (saveToGallery) {
26+
var picturePath = android.os.Environment.getExternalStoragePublicDirectory(
27+
android.os.Environment.DIRECTORY_PICTURES).getAbsolutePath() + "/" + "cameraPicture_" + dateStamp + ".jpg";
28+
var nativeFile = new java.io.File(picturePath);
29+
var tempPictureUri = android.net.Uri.fromFile(nativeFile);
30+
takePictureIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, tempPictureUri);
31+
} else {
32+
var picturePath = utils.ad.getApplicationContext().getExternalFilesDir(null).getAbsolutePath() + "/" + "cameraPicture_" + dateStamp + ".jpg";
33+
var nativeFile = new java.io.File(picturePath);
34+
var tempPictureUri = android.net.Uri.fromFile(nativeFile);
35+
takePictureIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, tempPictureUri);
36+
}
2637

27-
var tempPicturePath = fileSystem.path.join(utils.ad.getApplicationContext().getExternalFilesDir(null).getAbsolutePath(), "cameraPicture_" + dateStamp + ".jpg");
28-
var nativeFile = new java.io.File(tempPicturePath);
29-
var tempPictureUri = android.net.Uri.fromFile(nativeFile);
30-
takePictureIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, tempPictureUri);
3138
if (takePictureIntent.resolveActivity(utils.ad.getApplicationContext().getPackageManager()) != null) {
3239

3340
var appModule: typeof applicationModule = require("application");
@@ -37,50 +44,54 @@ export var takePicture = function (options?): Promise<any> {
3744
appModule.android.onActivityResult = previousResult;
3845

3946
if (requestCode === REQUEST_IMAGE_CAPTURE && resultCode === android.app.Activity.RESULT_OK) {
40-
var options = new android.graphics.BitmapFactory.Options();
41-
options.inJustDecodeBounds = true;
42-
android.graphics.BitmapFactory.decodeFile(tempPicturePath, options);
43-
44-
var sampleSize = calculateInSampleSize(options.outWidth, options.outHeight, reqWidth, reqHeight);
45-
46-
var finalBitmapOptions = new android.graphics.BitmapFactory.Options();
47-
finalBitmapOptions.inSampleSize = sampleSize;
48-
var bitmap = android.graphics.BitmapFactory.decodeFile(tempPicturePath, finalBitmapOptions);
49-
var scaledSizeImage = null;
50-
if (reqHeight > 0 && reqWidth > 0) {
51-
if (shouldKeepAspectRatio) {
52-
53-
var common: typeof cameraCommonModule = require("./camera-common");
54-
55-
var aspectSafeSize = common.getAspectSafeDimensions(bitmap.getWidth(), bitmap.getHeight(), reqWidth, reqHeight);
56-
scaledSizeImage = android.graphics.Bitmap.createScaledBitmap(bitmap, aspectSafeSize.width, aspectSafeSize.height, true);
47+
var imageSource: typeof imageSourceModule = require("image-source");
48+
if (saveToGallery) {
49+
resolve({image:imageSource.fromFile(picturePath) ,path:picturePath});
50+
} else {
51+
var options = new android.graphics.BitmapFactory.Options();
52+
options.inJustDecodeBounds = true;
53+
android.graphics.BitmapFactory.decodeFile(picturePath, options);
54+
55+
var sampleSize = calculateInSampleSize(options.outWidth, options.outHeight, reqWidth, reqHeight);
56+
57+
var finalBitmapOptions = new android.graphics.BitmapFactory.Options();
58+
finalBitmapOptions.inSampleSize = sampleSize;
59+
var bitmap = android.graphics.BitmapFactory.decodeFile(picturePath, finalBitmapOptions);
60+
var scaledSizeImage = null;
61+
if (reqHeight > 0 && reqWidth > 0) {
62+
if (shouldKeepAspectRatio) {
63+
64+
var common: typeof cameraCommonModule = require("./camera-common");
65+
66+
var aspectSafeSize = common.getAspectSafeDimensions(bitmap.getWidth(), bitmap.getHeight(), reqWidth, reqHeight);
67+
scaledSizeImage = android.graphics.Bitmap.createScaledBitmap(bitmap, aspectSafeSize.width, aspectSafeSize.height, true);
68+
}
69+
else {
70+
scaledSizeImage = android.graphics.Bitmap.createScaledBitmap(bitmap, reqWidth, reqHeight, true);
71+
}
5772
}
5873
else {
59-
scaledSizeImage = android.graphics.Bitmap.createScaledBitmap(bitmap, reqWidth, reqHeight, true);
74+
scaledSizeImage = bitmap;
6075
}
61-
}
62-
else {
63-
scaledSizeImage = bitmap;
64-
}
6576

66-
var ei = new android.media.ExifInterface(tempPicturePath);
67-
var orientation = ei.getAttributeInt(android.media.ExifInterface.TAG_ORIENTATION, android.media.ExifInterface.ORIENTATION_NORMAL);
68-
69-
switch (orientation) {
70-
case android.media.ExifInterface.ORIENTATION_ROTATE_90:
71-
scaledSizeImage = rotateBitmap(scaledSizeImage, 90);
72-
break;
73-
case android.media.ExifInterface.ORIENTATION_ROTATE_180:
74-
scaledSizeImage = rotateBitmap(scaledSizeImage, 180);
75-
break;
76-
case android.media.ExifInterface.ORIENTATION_ROTATE_270:
77-
scaledSizeImage = rotateBitmap(scaledSizeImage, 270);
78-
break;
79-
}
77+
var ei = new android.media.ExifInterface(picturePath);
78+
var orientation = ei.getAttributeInt(android.media.ExifInterface.TAG_ORIENTATION, android.media.ExifInterface.ORIENTATION_NORMAL);
79+
80+
switch (orientation) {
81+
case android.media.ExifInterface.ORIENTATION_ROTATE_90:
82+
scaledSizeImage = rotateBitmap(scaledSizeImage, 90);
83+
break;
84+
case android.media.ExifInterface.ORIENTATION_ROTATE_180:
85+
scaledSizeImage = rotateBitmap(scaledSizeImage, 180);
86+
break;
87+
case android.media.ExifInterface.ORIENTATION_ROTATE_270:
88+
scaledSizeImage = rotateBitmap(scaledSizeImage, 270);
89+
break;
90+
}
8091

81-
var imageSource: typeof imageSourceModule = require("image-source");
92+
resolve({image:imageSource.fromNativeSource(scaledSizeImage), path:picturePath});
8293

83-
resolve(imageSource.fromNativeSource(scaledSizeImage));
94+
}
8495
}
8596
};
8697

@@ -123,4 +134,4 @@ var rotateBitmap = function (source, angle) {
123134
var matrix = new android.graphics.Matrix();
124135
matrix.postRotate(angle);
125136
return android.graphics.Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
126-
}
137+
}

0 commit comments

Comments
 (0)
0