(Android) Fix NullPointerException error on some Android phones (#429)

This commit is contained in:
Hazem Saleh 2019-05-05 13:16:20 -04:00 committed by Jan Piotrowski
parent fae190ead2
commit 295e928784

View File

@ -303,7 +303,7 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
this.imageUri = new CordovaUri(FileProvider.getUriForFile(cordova.getActivity(), this.imageUri = new CordovaUri(FileProvider.getUriForFile(cordova.getActivity(),
applicationId + ".provider", applicationId + ".provider",
photo)); photo));
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageUri.getCorrectUri()); intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri.getCorrectUri());
//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); intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
@ -387,7 +387,7 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
} }
File photo = createCaptureFile(JPEG); File photo = createCaptureFile(JPEG);
croppedUri = Uri.fromFile(photo); croppedUri = Uri.fromFile(photo);
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, croppedUri); intent.putExtra(MediaStore.EXTRA_OUTPUT, croppedUri);
} else { } else {
intent.setAction(Intent.ACTION_GET_CONTENT); intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE); intent.addCategory(Intent.CATEGORY_OPENABLE);
@ -687,7 +687,7 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
int rotate = 0; int rotate = 0;
String fileLocation = FileHelper.getRealPath(uri, this.cordova); String fileLocation = FileHelper.getRealPath(uri, this.cordova);
LOG.d(LOG_TAG, "File locaton is: " + fileLocation); LOG.d(LOG_TAG, "File location is: " + fileLocation);
String uriString = uri.toString(); String uriString = uri.toString();
String mimeType = FileHelper.getMimeType(uriString, this.cordova); String mimeType = FileHelper.getMimeType(uriString, this.cordova);
@ -703,7 +703,7 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
// rotating, nor compressing needs to be done // rotating, nor compressing needs to be done
if (this.targetHeight == -1 && this.targetWidth == -1 && if (this.targetHeight == -1 && this.targetWidth == -1 &&
(destType == FILE_URI || destType == NATIVE_URI) && !this.correctOrientation && (destType == FILE_URI || destType == NATIVE_URI) && !this.correctOrientation &&
mimeType.equalsIgnoreCase(getMimetypeForFormat(encodingType))) mimeType != null && mimeType.equalsIgnoreCase(getMimetypeForFormat(encodingType)))
{ {
this.callbackContext.success(uriString); this.callbackContext.success(uriString);
} else { } else {
@ -910,14 +910,14 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
*/ */
private Uri getUriFromMediaStore() { private Uri getUriFromMediaStore() {
ContentValues values = new ContentValues(); ContentValues values = new ContentValues();
values.put(android.provider.MediaStore.Images.Media.MIME_TYPE, JPEG_MIME_TYPE); values.put(MediaStore.Images.Media.MIME_TYPE, JPEG_MIME_TYPE);
Uri uri; Uri uri;
try { try {
uri = this.cordova.getActivity().getContentResolver().insert(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); uri = this.cordova.getActivity().getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
} catch (RuntimeException e) { } catch (RuntimeException e) {
LOG.d(LOG_TAG, "Can't write to external media storage."); LOG.d(LOG_TAG, "Can't write to external media storage.");
try { try {
uri = this.cordova.getActivity().getContentResolver().insert(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI, values); uri = this.cordova.getActivity().getContentResolver().insert(MediaStore.Images.Media.INTERNAL_CONTENT_URI, values);
} catch (RuntimeException ex) { } catch (RuntimeException ex) {
LOG.d(LOG_TAG, "Can't write to internal media storage."); LOG.d(LOG_TAG, "Can't write to internal media storage.");
return null; return null;
@ -1243,9 +1243,9 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
*/ */
private Uri whichContentStore() { private Uri whichContentStore() {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
return android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI; return MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else { } else {
return android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI; return MediaStore.Images.Media.INTERNAL_CONTENT_URI;
} }
} }
@ -1297,7 +1297,7 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
public void onMediaScannerConnected() { public void onMediaScannerConnected() {
try { try {
this.conn.scanFile(this.scanMe.toString(), "image/*"); this.conn.scanFile(this.scanMe.toString(), "image/*");
} catch (java.lang.IllegalStateException e) { } catch (IllegalStateException e) {
LOG.e(LOG_TAG, "Can't scan file in MediaScanner after taking picture"); LOG.e(LOG_TAG, "Can't scan file in MediaScanner after taking picture");
} }