2012-05-25 46 views
1

我从SD卡读取图像并将数组中的路径存储在活动内的数组中,并且工作正常,并且此读数位于方法内。第二次,我从另一个活动调用相同的方法,但游标返回空........我可以说问题是与“这个”,但不知道在哪里改变。下面的方法的代码。第二次从数据库中读取图像时出现空指针异常

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 
+0

链接什么是IS.LoadImagesFromSDCard IS(); ..... .... –

+0

你可以附加你的logcat吗? – Tony

+0

IS是活动的对象,它具有方法LoadFrom ...().... – jxgn

回答

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

相关问题