Search code examples
androidandroid-imageandroid-external-storage

Android null pointer exception with camera images


i am trying to launch the camera and capture the image file in the inactivity result then save it in a file.. but i keep getting a java.lang.NullPointerException at the getContentResolver.notifyChange line.

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

    switch(requestCode) {
        case 1001:
            if(resultCode == Activity.RESULT_OK) {
                getContentResolver().notifyChange(imageUri, null);
                imageView = (ImageView )findViewById(R.id.take_photo_image_view);
                ContentResolver contentResolver = getContentResolver();
                Bitmap bitmap;
                try {
                    bitmap = android.provider.MediaStore.Images.Media.getBitmap(contentResolver,
                      imageUri);
                    imageView.setImageBitmap(bitmap);
                } catch(Exception e) {
                    Toast.makeText(GetPhotoActivity.this, "failed to load",
                    Toast.LENGTH_LONG).show();
                    Log.e(LOG_TAG, e.toString());
                }
            }
    }
}

11-23 17:26:00.508  32727-32727/com.example.testspotter E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.testspotter, PID: 32727
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1001, result=-1, data=null} to activity {com.example.testspotter/com.example.testspotter.GetPhotoActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.deliverResults(ActivityThread.java:3942)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:3992)
        at android.app.ActivityThread.access$1300(ActivityThread.java:156)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1403)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:157)
        at android.app.ActivityThread.main(ActivityThread.java:5872)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:852)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:668)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at android.os.Parcel.readException(Parcel.java:1480)
        at android.os.Parcel.readException(Parcel.java:1428)
        at android.content.IContentService$Stub$Proxy.notifyChange(IContentService.java:486)
        at android.content.ContentResolver.notifyChange(ContentResolver.java:1668)
        at android.content.ContentResolver.notifyChange(ContentResolver.java:1657)
        at android.content.ContentResolver.notifyChange(ContentResolver.java:1637)
        at com.example.testspotter.GetPhotoActivity.onActivityResult(GetPhotoActivity.java:126)

Solution

  • Call for camera activity using below code:

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        File f = new File(android.os.Environment
                                .getExternalStorageDirectory(), "temp.jpg");
                        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
                        startActivityForResult(intent, REQUEST_CAMERA);

    In onActivityResult

    if (resultCode == RESULT_OK) {
                if (requestCode == REQUEST_CAMERA) {
                    File f = new File(Environment.getExternalStorageDirectory()
                            .toString());
                    for (File temp : f.listFiles()) {
                        if (temp.getName().equals("temp.jpg")) {
                            f = temp;
                            break;
                        }
                    }
                    try {
                        Bitmap bm;
                        BitmapFactory.Options btmapOptions = new BitmapFactory.Options();
                        imageView = (ImageView )findViewById(R.id.take_photo_image_view);
                        bm = BitmapFactory.decodeFile(f.getAbsolutePath(),
                                btmapOptions);
     
                        // bm = Bitmap.createScaledBitmap(bm, 70, 70, true);
                        imageView.setImageBitmap(bm);
     
                        String path = android.os.Environment
                                .getExternalStorageDirectory()
                                + File.separator
                                + "Phoenix" + File.separator + "default";
                        f.delete();
                        OutputStream fOut = null;
                        File file = new File(path, String.valueOf(System
                                .currentTimeMillis()) + ".jpg");
                        try {
                            fOut = new FileOutputStream(file);
                            bm.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
                            fOut.flush();
                            fOut.close();
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }