Search code examples
javaandroidimageandroid-intentbitmap

Android App Crash After Several Image Taking


I am developing an application for Android which is supposed to take pictures from camera and upload those images to database. However, the app runs fine for first 8-10 pictures, but when I attempt to take more pictures the app gets crashed.

The error from log

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.project.salesservice, PID: 18379
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.project.salesservice/com.project.salesservice.user.UserVisitDetailActivity}: java.lang.NullPointerException: uriString
    at android.app.ActivityThread.deliverResults(ActivityThread.java:5195)
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:5236)
    at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2187)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:236)
    at android.app.ActivityThread.main(ActivityThread.java:8057)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:620)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1011)
 Caused by: java.lang.NullPointerException: uriString
    at android.net.Uri$StringUri.<init>(Uri.java:496)
    at android.net.Uri$StringUri.<init>(Uri.java:486)
    at android.net.Uri.parse(Uri.java:458)
    at com.project.salesservice.user.UserVisitDetailActivity.getImageUri(UserVisitDetailActivity.java:582)
    at com.project.salesservice.user.UserVisitDetailActivity.onActivityResult(UserVisitDetailActivity.java:558)
    at android.app.Activity.dispatchActivityResult(Activity.java:8516)
    at android.app.ActivityThread.deliverResults(ActivityThread.java:5188)
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:5236) 
    at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2187) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:236) 
    at android.app.ActivityThread.main(ActivityThread.java:8057) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:620) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1011) 

Code for starting intent

    imageUri = null;
    Intent takePictureIntent = new Intent();
    takePictureIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageUri);
    takePictureIntent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);

On Activity Result Code

@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode == REQUEST_IMAGE_CAPTURE && resultCode == UserVisitDetailActivity.RESULT_OK && data != null){
        System.out.println("Masuk Camera");

        Bitmap photo = (Bitmap) data.getExtras().get("data");

        imageUri = getImageUri(getApplicationContext(), photo);

        if(checkInOut.getText().equals("Check In")) {
            System.out.println(imageUri);
            checkInHolder.setVisibility(View.VISIBLE);
            checkInHolder.setImageURI(imageUri);
        } else if(checkInHolder.getVisibility() == View.VISIBLE && checkInOut.getText().equals("Check Out")) {
            System.out.println(imageUri);
            checkOutHolder.setVisibility(View.VISIBLE);
            checkOutHolder.setImageURI(imageUri);
        }
    }
}

Get Image URI Code

public Uri getImageUri(Context inContext, Bitmap inImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
    return Uri.parse(path);
}

The problem is solved when the photos stored in the gallery are deleted. And the app will start to crash again if the photos reach 10 in gallery. Sorry if my grammar is not correct. Really appreciate for your help. Thank you


Solution

  • Maybe you can optimize your image taking. The lack of memory on the device may be one of the cause of this problem. If there is not enough memory, the program may crash since taking pictures uses up memory. You might think about compressing the pictures before submitting them to the database or putting in place a system that releases memory after each picture is taken to solve this problem.

    You could also check if the call to the MediaStore.Images.Media.insertImage method returns an empty string which is then passed to the Uri.parse method causing a NullPointerException. You should check if the MediaStore.Images.Media.insertImage method actually returns a valid value. If it does not, you should consider using another method to insert the image into the internal storage.