Here is methods for open camera. I dont know the specyfic starting of this error, sometimes i got this error and sometimes didnt. I dont know why my stupid teammate use the spaghetti code for getting the image from camera, but i need resolve it.
So why i think about add other resultCode for handle something wrong What are u guys think?
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
if (requestCode == REQUEST_TAKE_PHOTO) {
Uri imageUri = Uri.parse(photoAbsolutePath);
onPhotoResult(imageUri);
} else if (requestCode == REQUEST_OPEN_GALLERY) {
Uri imageUri = data.getData();
onPhotoResult(imageUri);
}
}
}
protected void onPhotoResult(Uri uriPath) {
Bitmap bitmap = Tools.loadBitmapFromGallery(uriPath, getActivity());
if (bitmap != null) {
float angle = Tools.getRotation(uriPath, getActivity());
if (angle != 0f) {
bitmap = Tools.rotate(bitmap, angle);
}
RequestUploadPhotos requestUploadPhotos = new RequestUploadPhotos();
byte[] photo = new Tools().getImageData(bitmap);
try {
File tempFile = File.createTempFile("temp", ".jpg");
if (tempFile != null) {
FileOutputStream fos = new FileOutputStream(tempFile);
fos.write(photo);
fos.close();
requestUploadPhotos.photoFilesList.add(tempFile);
}
} catch (IOException e) {
e.printStackTrace();
}
if (requestUploadPhotos.photoFilesList != null) {
UploadPhoto uploadPhoto = new UploadPhoto();
uploadPhoto.execute(requestUploadPhotos)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(response -> {
if (response.getSuccess()) {
List<ImageUrl> addedPhotos = new ArrayList<>();
for (int i = 0; i < response.newGalleryPhotos.size(); i++) {
addedPhotos.add(new ImageUrl(
response.newGalleryPhotos.get(i).getImageId(),
response.newGalleryPhotos.get(i).getImageUrl()));
}
myGalleryAdapter.addPhotosToGallery(addedPhotos);
} else {
Toast.makeText(getContext(), response.getActionRes(), Toast.LENGTH_SHORT).show();
}
}, throwable -> throwable.printStackTrace(), () -> {
});
} else {
Toast.makeText(getContext(), "Error forming request", Toast.LENGTH_SHORT).show();
}
}
}
java.lang.RuntimeException: Unable to resume activity {.NewMainActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65536, result=-1, data=null} to activity {NewMainActivity}: java.lang.NullPointerException: uriString
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3176)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3207)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2544)
at android.app.ActivityThread.access$900(ActivityThread.java:168)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1378)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:5665)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:689)
Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65536, result=-1, data=null} to activity .NewMainActivity}: java.lang.NullPointerException: uriString
at android.app.ActivityThread.deliverResults(ActivityThread.java:3778)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3163)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3207)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2544)
at android.app.ActivityThread.access$900(ActivityThread.java:168)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1378)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:5665)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:689)
Caused by: java.lang.NullPointerException: uriString
at android.net.Uri$StringUri.<init>(Uri.java:477)
at android.net.Uri$StringUri.<init>(Uri.java:467)
at android.net.Uri.parse(Uri.java:439)
at co.spontime.ui.fragments.UserProfileFragment.onActivityResult(UserProfileFragment.java:307)
Edited:
@RequiresPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
private void dispatchTakePicture() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
Log.i("DispatchTakePicture", "file created:" + photoFile.getAbsolutePath());
} catch (IOException ex) {
// Error occurred while creating the File
Log.e(this.getClass().getName(), "Error, while creating a directory \n" +
ex.toString());
}
// Continue only if the File was successfully created
if (photoFile != null) {
try {
photoAbsolutePath = "file:" + photoFile.getAbsolutePath();
Uri photoUri = FileProvider.getUriForFile(getContext(),
"co.spontime.fileprovider", photoFile);
getContext().grantUriPermission("com.android.camera", photoUri,
Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
//you must setup this
takePictureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
takePictureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
} catch (IllegalArgumentException e) {
Log.e("TakePhoto", "Failed providing Uri from FileProvider");
e.printStackTrace();
}
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
Yes beacuse inside onActivityResult method your using Uri imageUri = Uri.parse(photoAbsolutePath); where photoAbsolutePath may be null.
you should get uri from data intent.Apart from it you should use same request code whatever you have taken at the time of startActivityForResult.