null pointer exception in android camera applicati

2019-08-29 03:31发布

问题:

I am trying to save a picture in an Android application. Here is my callback function:

ShutterCallback myShutterCallback = new ShutterCallback(){

    @Override
    public void onShutter() {
        Log.d(TAG, "shutter callback");

    }};


PictureCallback myPictureCallback_RAW = new PictureCallback(){

    @Override
    public void onPictureTaken(byte[] arg0, Camera arg1) {
        Log.d(TAG, "raw callback");

    }};

PictureCallback myPictureCallback_JPG = new PictureCallback(){

    @Override
    public void onPictureTaken(byte[] arg0, Camera arg1) {
        // TODO Auto-generated method stub
        Bitmap bitmapPicture 
            = BitmapFactory.decodeByteArray(arg0, 0, arg0.length);  

        ContentValues values = new ContentValues();
        values.put(Images.Media.TITLE, "title");
        values.put(Images.Media.BUCKET_ID, "test");
        values.put(Images.Media.DESCRIPTION, "test Image taken");
        values.put(Images.Media.MIME_TYPE, "image/jpeg");

        Uri uriTarget = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);

        OutputStream imageFileOS;
        try {
            imageFileOS = getContentResolver().openOutputStream(uriTarget);
            imageFileOS.write(arg0);
            imageFileOS.flush();
            imageFileOS.close();

            Toast.makeText(CameraView.this, 
                    "Image saved: " + uriTarget.toString(), 
                    Toast.LENGTH_LONG).show();

        } catch (FileNotFoundException e) {
            Log.d(TAG, "file not found " + e.getMessage());
            e.printStackTrace();
        } catch (IOException e) {
            Log.d(TAG, "io exception " + e.getMessage());
            e.printStackTrace();
        }

        //camera.startPreview();
        finish();
    }
};

The problem is, for several different types of phones, I get a NullPointerException at this line:

imageFileOS = getContentResolver().openOutputStream(uriTarget);

Has anyone run into this before?

Edit - Stack trace

04-24 11:52:03.979: E/AndroidRuntime(12695): FATAL EXCEPTION: main
04-24 11:52:03.979: E/AndroidRuntime(12695): java.lang.NullPointerException
04-24 11:52:03.979: E/AndroidRuntime(12695):    at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:465)
04-24 11:52:03.979: E/AndroidRuntime(12695):    at android.content.ContentResolver.openOutputStream(ContentResolver.java:361)
04-24 11:52:03.979: E/AndroidRuntime(12695):    at android.content.ContentResolver.openOutputStream(ContentResolver.java:337)
04-24 11:52:03.979: E/AndroidRuntime(12695):    at com.appfury.grafighters.CameraView$4.onPictureTaken(CameraView.java:234)
04-24 11:52:03.979: E/AndroidRuntime(12695):    at android.hardware.Camera$EventHandler.handleMessage(Camera.java:536)
04-24 11:52:03.979: E/AndroidRuntime(12695):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-24 11:52:03.979: E/AndroidRuntime(12695):    at android.os.Looper.loop(Looper.java:150)
04-24 11:52:03.979: E/AndroidRuntime(12695):    at android.app.ActivityThread.main(ActivityThread.java:4263)
04-24 11:52:03.979: E/AndroidRuntime(12695):    at java.lang.reflect.Method.invokeNative(Native Method)
04-24 11:52:03.979: E/AndroidRuntime(12695):    at java.lang.reflect.Method.invoke(Method.java:507)
04-24 11:52:03.979: E/AndroidRuntime(12695):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-24 11:52:03.979: E/AndroidRuntime(12695):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-24 11:52:03.979: E/AndroidRuntime(12695):    at dalvik.system.NativeStart.main(Native Method)

回答1:

It looks similar to this question. Check your logcat if there is any exceptions thrown by media provider and use debugger to check if uriTarget is null when you calling imageFileOS = getContentResolver().openOutputStream(uriTarget);



回答2:

It turns out the problem was specific to HTC devices. I resolved the problem by saving the image files in the DCIM folder on the device, but not using getContentResolver to get the path.