本文介绍了在Android的相机应用空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  ShutterCallback myShutterCallback =新ShutterCallback(){    @覆盖
    公共无效onShutter(){
        Log.d(TAG,快门回调);    }};
PictureCallback myPictureCallback_RAW =新PictureCallback(){    @覆盖
    公共无效onPictureTaken(字节[]为arg0,ARG1相机){
        Log.d(TAG,原始回调);    }};PictureCallback myPictureCallback_JPG =新PictureCallback(){    @覆盖
    公共无效onPictureTaken(字节[]为arg0,ARG1相机){
        // TODO自动生成方法存根
        位图bitmapPicture
            = BitmapFactory.de codeByteArray的(为arg0,0,arg0.length);        ContentValues​​值=新ContentValues​​();
        values​​.put(Images.Media.TITLE,称号);
        values​​.put(Images.Media.BUCKET_ID,测试);
        values​​.put(Images.Media.DESCRIPTION,测试图像采取);
        values​​.put(Images.Media.MIME_TYPE,图像/ JPEG);        乌里uriTarget = getContentResolver()插入(Media.EXTERNAL_CONTENT_URI,价值观)。        的OutputStream imageFileOS;
        尝试{
            imageFileOS = getContentResolver()openOutputStream(uriTarget)。
            imageFileOS.write(为arg0);
            imageFileOS.flush();
            imageFileOS.close();            Toast.makeText(CameraView.this,
                    图像保存:+ uriTarget.toString()
                    Toast.LENGTH_LONG).show();        }赶上(FileNotFoundException异常五){
            Log.d(TAG,找不到文件+ e.getMessage());
            e.printStackTrace();
        }赶上(IOException异常五){
            Log.d(TAGIO异常+ e.getMessage());
            e.printStackTrace();
        }        //camera.start$p$pview();
        完();
    }
};

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

  imageFileOS = getContentResolver()openOutputStream(uriTarget)。

有没有人遇到此之前?

修改 - 堆栈跟踪

  11月4日至24日:52:03.979:E / AndroidRuntime(12695):致命异常:主要
11月4日至24日:52:03.979:E / AndroidRuntime(12695):显示java.lang.NullPointerException
11月4日至24日:52:03.979:E / AndroidRuntime(12695):在android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:465)
11月4日至24日:52:03.979:E / AndroidRuntime(12695):在android.content.ContentResolver.openOutputStream(ContentResolver.java:361)
11月4日至24日:52:03.979:E / AndroidRuntime(12695):在android.content.ContentResolver.openOutputStream(ContentResolver.java:337)
11月4日至24日:52:03.979:E / AndroidRuntime(12695):在com.appfury.grafighters.CameraView $ 4.onPictureTaken(CameraView.java:234)
11月4日至24日:52:03.979:E / AndroidRuntime(12695):在android.hardware.Camera $ EventHandler.handleMessage(Camera.java:536)
11月4日至24日:52:03.979:E / AndroidRuntime(12695):在android.os.Handler.dispatchMessage(Handler.java:99)
11月4日至24日:52:03.979:E / AndroidRuntime(12695):在android.os.Looper.loop(Looper.java:150)
11月4日至24日:52:03.979:E / AndroidRuntime(12695):在android.app.ActivityThread.main(ActivityThread.java:4263)
11月4日至24日:52:03.979:E / AndroidRuntime(12695):在java.lang.reflect.Method.invokeNative(本机方法)
11月4日至24日:52:03.979:E / AndroidRuntime(12695):在java.lang.reflect.Method.invoke(Method.java:507)
11月4日至24日:52:03.979:E / AndroidRuntime(12695):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
11月4日至24日:52:03.979:E / AndroidRuntime(12695):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11月4日至24日:52:03.979:E / AndroidRuntime(12695):在dalvik.system.NativeStart.main(本机方法)


解决方案

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

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)
解决方案

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.

这篇关于在Android的相机应用空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 20:24