第二次从数据库读取图像时出现空指针异常

时间:2012-05-25 10:00:32

标签: android database nullpointerexception android-sdcard

我从我的SD卡中读取图像并将路径存储在活动内的数组中,并且它工作正常并且此读取位于方法内。第二次,我从另一个活动调用相同的方法,但光标返回null ........我可以说问题是“这个”,但无法弄清楚在哪里改变。在方法的代码下面。

 public void LoadImagesFromSDCard()
{
    try
    {
        String[] projection = {MediaStore.Images.Media.DATA};           

        ContentResolver cr = this.getContentResolver();

        cursor = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,projection,null,null,null);
        imageCount = cursor.getCount();
        imagePath = new String[imageCount + 1];
        cursor.moveToFirst();

        int cursor_index = 0;
        do
        {
                int id = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
                imagePath[cursor_index++] = cursor.getString(id);

        }while(cursor.moveToPosition(cursor_index));

     ........

我第二次从另一个活动中称之为

    Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
    public void onPictureTaken(byte[] imageData, Camera c) {


        if (imageData != null) {

            Intent mIntent = new Intent();

            StoreByteImage(mContext, imageData, 50,"ImageName");
            mCamera.startPreview();
            setResult(FOTO_MODE, mIntent);
            CameraTaken = true;

            IS.LoadImagesFromSDCard();
            finish();


        }
    }
};
实际上这是信息的相机活动。在此先感谢您的帮助:)

这里是logcat

E/CameraTest(19169): onCreate
E/CameraTest(19169): onResume
E/CameraTest(19169): surfaceCreated
D/CameraHardwareStub(   34): initHeapLocked: preview size=320x240
E/CameraTest(19169): surfaceChanged
D/CameraHardwareStub(   34): initHeapLocked: preview size=320x240
I/ActivityManager(   59): Displayed activity com.android.print/.CameraActivity: 1013   ms (total 1013 ms)
D/AudioSink(   34): bufferCount (4) is too small and increased to 12
I/global  (19169): Default buffer size used in BufferedOutputStream constructor. It would be better to be explicit if an 8k buffer is required.
E/Error reading file(19169): java.lang.NullPointerException
E/CameraTest(19169): surfaceDestroyed
E/CameraTest(19169): onStop

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

好的,我已经解决了这个问题,我想,因为我所做的就是工作。 关键是我的第一个活动是在后台,我从第一个调用相机活动,这使得第一个活动变为背景。所以我不得不添加android:launchMode =" singleTask"清单文件中的第一个活动。然后我明确地调用第一个在后台运行而不是 IS.LoadImagesFromSDCard(); 的活动。这样做会调用第一个活动中的onNewIntent()而不是onCreate()。在onNewIntent()中我调用了我的LoadFrom ...()方法。

    Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
    public void onPictureTaken(byte[] imageData, Camera c) {


        if (imageData != null) {

            Intent mIntent = new Intent();
            Intent inent = new Intent();

            StoreByteImage(mContext, imageData, 50,"ImageName");
            mCamera.startPreview();
            setResult(FOTO_MODE, mIntent);
            CameraTaken = true;
            ImageSelectionIntent.setClassName("your package name", "your package name + class name");
            startActivity(intent);
            finish();

我的第一个活动有以下代码

 protected void onNewIntent(Intent intent) {
      super.onNewIntent(intent);
      setIntent(intent);        
      LoadImagesFromSDCard();
    }
        }
    }
};

参考下面的链接

communicating between two activities while the one called the other still running in background