8000 fix(android): Android 11 camera support · NativeScript/plugins@991d31a · GitHub
[go: up one dir, main page]

Skip to content

Commit 991d31a

Browse files
committed
fix(android): Android 11 camera support
1 parent 7921d59 commit 991d31a

File tree

1 file changed

+66
-66
lines changed

1 file changed

+66
-66
lines changed

packages/camera/index.android.ts

Lines changed: 66 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -80,81 +80,81 @@ export let takePicture = function (options?): Promise<any> {
8080
takePictureIntent.putExtra('android.intent.extras.CAMERA_FACING', android.hardware.Camera.CameraInfo.CAMERA_FACING_BACK);
8181
}
8282

83-
if (takePictureIntent.resolveActivity(Utils.android.getApplicationContext().getPackageManager()) != null) {
84-
// Remove previous listeners if any
85-
Application.android.off('activityResult');
86-
87-
Application.android.on('activityResult', (args) => {
88-
const requestCode = args.requestCode;
89-
const resultCode = args.resultCode;
90-
91-
if (requestCode === REQUEST_IMAGE_CAPTURE && resultCode === android.app.Activity.RESULT_OK) {
92-
const currentTimeMillis = java.lang.Long.valueOf(java.lang.System.currentTimeMillis());
93-
const values = new android.content.ContentValues();
94-
values.put(android.provider.MediaStore.MediaColumns.DISPLAY_NAME, fileName);
95-
values.put((android as any).provider.MediaStore.Images.Media.DATE_ADDED, currentTimeMillis);
96-
values.put(android.provider.MediaStore.MediaColumns.MIME_TYPE, 'image/*');
97-
if (sdkVersionInt >= 29) {
98-
values.put(android.provider.MediaStore.MediaColumns.RELATIVE_PATH, android.os.Environment.DIRECTORY_DCIM);
99-
values.put(android.provider.MediaStore.MediaColumns.IS_PENDING, java.lang.Integer.valueOf(1));
100-
values.put((android as any).provider.MediaStore.Images.Media.DATE_TAKEN, currentTimeMillis);
101-
}
83+
// Remove previous listeners if any
84+
Application.android.off('activityResult');
85+
86+
Application.android.on('activityResult', (args) => {
87+
const requestCode = args.requestCode;
88+
const resultCode = args.resultCode;
89+
90+
if (requestCode === REQUEST_IMAGE_CAPTURE && resultCode === android.app.Activity.RESULT_OK) {
91+
const currentTimeMillis = java.lang.Integer.valueOf(java.lang.System.currentTimeMillis());
92+
const values = new android.content.ContentValues();
93+
values.put(android.provider.MediaStore.MediaColumns.DISPLAY_NAME, fileName);
94+
values.put(android.provider.MediaStore.MediaColumns.DATE_ADDED, currentTimeMillis);
95+
values.put(android.provider.MediaStore.MediaColumns.DATE_MODIFIED, currentTimeMillis);
96+
values.put(android.provider.MediaStore.MediaColumns.MIME_TYPE, 'image/*');
97+
98+
if (sdkVersionInt >= 29) {
99+
values.put(android.provider.MediaStore.MediaColumns.RELATIVE_PATH, android.os.Environment.DIRECTORY_DCIM);
100+
values.put(android.provider.MediaStore.MediaColumns.IS_PENDING, java.lang.Integer.valueOf(1));
101+
values.put(android.provider.MediaStore.MediaColumns.DATE_TAKEN, currentTimeMillis);
102+
}
102103

103-
const uri = Utils.android.getApplicationContext().getContentResolver().insert(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
104-
105-
if (saveToGallery) {
106-
const fos: java.io.FileOutputStream = Utils.android.getApplicationContext().getContentResolver().openOutputStream(uri);
107-
const fis = new java.io.FileInputStream(nativeFile);
108-
try {
109-
(org as any).nativescript.plugins.camera.Utils.copy(fis, fos);
110-
if (sdkVersionInt >= 29) {
111-
values.clear();
112-
values.put((android as any).provider.MediaStore.Video.Media.IS_PENDING, java.lang.Integer.valueOf(0));
113-
Utils.android.getApplicationContext().contentResolver.update(uri, values, null, null);
114-
}
115-
} catch (e) {
116-
reject(e);
104+
const uri = Utils.android.getApplicationContext().getContentResolver().insert(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
105+
106+
if (saveToGallery) {
107+
const fos: java.io.FileOutputStream = Utils.android.getApplicationContext().getCon 57AE tentResolver().openOutputStream(uri);
108+
const fis = new java.io.FileInputStream(nativeFile);
109+
try {
110+
(org as any).nativescript.plugins.camera.Utils.copy(fis, fos);
111+
if (sdkVersionInt >= 29) {
112+
values.clear();
113+
values.put((android as any).provider.MediaStore.Video.Media.IS_PENDING, java.lang.Integer.valueOf(0));
114+
Utils.android.getApplicationContext().getContentResolver().update(uri, values, null, null);
117115
}
116+
} catch (e) {
117+
reject(e);
118118
}
119+
}
119120

120-
let exif = new android.media.ExifInterface(picturePath);
121-
let orientation = exif.getAttributeInt(android.media.ExifInterface.TAG_ORIENTATION, android.media.ExifInterface.ORIENTATION_NORMAL);
121+
let exif = new android.media.ExifInterface(picturePath);
122+
let orientation = exif.getAttributeInt(android.media.ExifInterface.TAG_ORIENTATION, android.media.ExifInterface.ORIENTATION_NORMAL);
122123

123-
if (orientation === android.media.ExifInterface.ORIENTATION_ROTATE_90) {
124-
rotateBitmap(picturePath, 90);
125-
} else if (orientation === android.media.ExifInterface.ORIENTATION_ROTATE_180) {
126-
rotateBitmap(picturePath, 180);
127-
} else if (orientation === android.media.ExifInterface.ORIENTATION_ROTATE_270) {
128-
rotateBitmap(picturePath, 270);
129-
}
124+
if (orientation === android.media.ExifInterface.ORIENTATION_ROTATE_90) {
125+
rotateBitmap(picturePath, 90);
126+
} else if (orientation === android.media.ExifInterface.ORIENTATION_ROTATE_180) {
127+
rotateBitmap(picturePath, 180);
128+
} else if (orientation === android.media.ExifInterface.ORIENTATION_ROTATE_270) {
129+
rotateBitmap(picturePath, 270);
130+
}
130131

131-
if (shouldKeepAspectRatio) {
132-
let pictureWidth = exif.getAttributeInt(android.media.ExifInterface.TAG_IMAGE_WIDTH, 0);
133-
let pictureHeight = exif.getAttributeInt(android.media.ExifInterface.TAG_IMAGE_LENGTH, 0);
134-
let isPictureLandscape = pictureWidth > pictureHeight;
135-
let areOptionsLandscape = reqWidth > reqHeight;
136-
if (isPictureLandscape !== areOptionsLandscape) {
137-
let oldReqWidth = reqWidth;
138-
reqWidth = reqHeight;
139-
reqHeight = oldReqWidth;
140-
}
132+
if (shouldKeepAspectRatio) {
133+
let pictureWidth = exif.getAttributeInt(android.media.ExifInterface.TAG_IMAGE_WIDTH, 0);
134+
let pictureHeight = exif.getAttributeInt(android.media.ExifInterface.TAG_IMAGE_LENGTH, 0);
135+
let isPictureLandscape = pictureWidth > pictureHeight;
136+
let areOptionsLandscape = reqWidth > reqHeight;
137+
if (isPictureLandscape !== areOptionsLandscape) {
138+
let oldReqWidth = reqWidth;
139+
reqWidth = reqHeight;
140+
reqHeight = oldReqWidth;
141141
}
142-
143-
let asset = new ImageAsset(picturePath);
144-
asset.options = {
145-
width: reqWidth,
146-
height: reqHeight,
147-
keepAspectRatio: shouldKeepAspectRatio,
148-
};
149-
resolve(asset);
150-
} else if (resultCode === android.app.Activity.RESULT_CANCELED) {
151-
// User cancelled the image capture
152-
reject(new Error('cancelled'));
153142
}
154-
});
155143

156-
(Application.android.foregroundActivity || Application.android.startActivity).startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
157-
}
144+
let asset = new ImageAsset(picturePath);
145+
asset.options = {
146+
width: reqWidth,
147+
height: reqHeight,
148+
keepAspectRatio: shouldKeepAspectRatio,
149+
};
150+
resolve(asset);
151+
} else if (resultCode === android.app.Activity.RESULT_CANCELED) {
152+
// User cancelled the image capture
153+
reject(new Error('cancelled'));
154+
}
155+
});
156+
157+
(Application.android.foregroundActivity || Application.android.startActivity).startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
158158
} catch (e) {
159159
if (reject) {
160160
reject(e);

0 commit comments

Comments
 (0)
0