From 53b7d8df84fce9debe2e1f07d98d7d1c334a3950 Mon Sep 17 00:00:00 2001 From: Max Date: Tue, 9 Nov 2021 10:14:24 +0800 Subject: [PATCH] (android) Close cursors to suppress log warnings "W/System: A resource failed to call close" is logged whenever a Cursor object is left unclosed in this file. `queryImgDB` "May return null if the underlying content provider returns null, or if it crashes." so check for null. --- src/android/CameraLauncher.java | 42 ++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/src/android/CameraLauncher.java b/src/android/CameraLauncher.java index 198c03999..00ea699fa 100644 --- a/src/android/CameraLauncher.java +++ b/src/android/CameraLauncher.java @@ -287,7 +287,11 @@ public void callTakePicture(int returnType, int encodingType) { public void takePicture(int returnType, int encodingType) { // Save the number of images currently on disk for later - this.numPics = queryImgDB(whichContentStore()).getCount(); + Cursor cursor = queryImgDB(whichContentStore()); + if (cursor != null) { + this.numPics = cursor.getCount(); + cursor.close(); + } // Let's use the intent and see what happens Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); @@ -299,13 +303,13 @@ public void takePicture(int returnType, int encodingType) applicationId + ".cordova.plugin.camera.provider", photo); intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); - //We can write to this URI, this will hopefully allow us to write files to get to the next step + // We can write to this URI, this will hopefully allow us to write files to get to the next step intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION); if (this.cordova != null) { // Let's check to make sure the camera is actually installed. (Legacy Nexus 7 code) PackageManager mPm = this.cordova.getActivity().getPackageManager(); - if(intent.resolveActivity(mPm) != null) + if (intent.resolveActivity(mPm) != null) { this.cordova.startActivityForResult((CordovaPlugin) this, intent, (CAMERA + 1) * 16 + returnType + 1); } @@ -520,7 +524,6 @@ private void processResultFromCamera(int destType, Intent intent) throws IOExcep return; } - this.processPicture(bitmap, this.encodingType); if (!this.saveToPhotoAlbum) { @@ -574,8 +577,8 @@ else if (destType == FILE_URI) { if (this.encodingType == JPEG) { String exifPath; exifPath = uri.getPath(); - //We just finished rotating it by an arbitrary orientation, just make sure it's normal - if(rotate != ExifInterface.ORIENTATION_NORMAL) + // We just finished rotating it by an arbitrary orientation, just make sure it's normal + if (rotate != ExifInterface.ORIENTATION_NORMAL) exif.resetOrientation(); exif.createOutFile(exifPath); exif.writeExifData(); @@ -800,7 +803,6 @@ private boolean isImageMimeTypeProcessable(String mimeType) { * @param intent An Intent, which can return result data to the caller (various data can be attached to Intent "extras"). */ public void onActivityResult(int requestCode, int resultCode, Intent intent) { - // Get src and dest types from request code for a Camera Activity int srcType = (requestCode / 16) - 1; int destType = (requestCode % 16) - 1; @@ -1225,21 +1227,23 @@ private void checkForDuplicateImage(int type) { int diff = 1; Uri contentStore = whichContentStore(); Cursor cursor = queryImgDB(contentStore); - int currentNumOfImages = cursor.getCount(); + if (cursor != null) { + int currentNumOfImages = cursor.getCount(); - if (type == FILE_URI && this.saveToPhotoAlbum) { - diff = 2; - } + if (type == FILE_URI && this.saveToPhotoAlbum) { + diff = 2; + } - // delete the duplicate file if the difference is 2 for file URI or 1 for Data URL - if ((currentNumOfImages - numPics) == diff) { - cursor.moveToLast(); - int id = Integer.valueOf(cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media._ID))); - if (diff == 2) { - id--; + // delete the duplicate file if the difference is 2 for file URI or 1 for Data URL + if ((currentNumOfImages - this.numPics) == diff) { + cursor.moveToLast(); + int id = Integer.valueOf(cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media._ID))); + if (diff == 2) { + id--; + } + Uri uri = Uri.parse(contentStore + "/" + id); + this.cordova.getActivity().getContentResolver().delete(uri, null, null); } - Uri uri = Uri.parse(contentStore + "/" + id); - this.cordova.getActivity().getContentResolver().delete(uri, null, null); cursor.close(); } }