在Android摄像头应用空指针异常(null pointer exception in andro

2019-10-16 15:14发布

我想保存的图片在Android应用程序。 这里是我的回调函数:

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();
    }
};

问题是,对几种不同类型的手机,我得到一个NullPointerException ,在这一行:

imageFileOS = getContentResolver().openOutputStream(uriTarget);

有没有人遇到此之前?

编辑 - 堆栈跟踪

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)

Answer 1:

它看起来类似这样的问题。 检查logcat的,如果有媒体提供商抛出的异常,并使用调试器来检查,如果uriTarget为空当你调用imageFileOS = getContentResolver().openOutputStream(uriTarget);



Answer 2:

原来,问题是具体到HTC设备。 我通过保存在设备上的DCIM文件夹的图像文件,但不使用getContentResolver拿到路径解决了这个问题。



文章来源: null pointer exception in android camera application