Android:方法onPictureTaken从未调用,即使使用AsyncTask也是如此

时间:2013-01-30 13:47:53

标签: android eclipse android-camera

我想 活动“GameActivity” 拍张照片 将图片保存在SD卡上 将图片与其他活动一起使用

问题是,当我只是保存图片并且不做任何操作时,图片会被保存。当我在camera.takePicture之后启动活动(方法startActivity)时,永远不会调用onPictureTaken方法。

我读过它是因为方法takePicture执行起来很长,所以我遵循了这个网站上的建议(http://www.workreloaded.com/2011/06/how-to-use-the-android-camera/)并创建了一个扩展AsyncTask的类

public class PhotoTask extends AsyncTask<Boolean, Void, Void> {
public PhotoTask(Camera camera, SurfaceView surfaceCamera,boolean isPreview, SurfaceHolder holder, GameActivity gameactivity) {
constructor
}

    @Override
protected Void doInBackground(Boolean... params) {
     camera.takePicture(null,null, myPictureCallback_JPG);
        Log.w("GAMEACTIVITY","TAKEPICTURE");
        return null;
}

    @Override
protected void onPostExecute(Void result) {
    Log.w("GAMEACTIVITY","INTENT");
     Intent intent = new Intent(gameactivity.getApplicationContext(),ShareActivity.class);
     // Sending the picture taken to ShareActivity
     intent.putExtra("PICTURE_TAKEN", output);
     gameactivity.startActivity(intent);
}


PictureCallback myPictureCallback_JPG = new PictureCallback(){

    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
        Log.w("GAMEACTIVITY","FIRST");
            File imagesFolder = new File(Environment.getExternalStorageDirectory(), "/KersplattFolder");
            imagesFolder.mkdirs(); 
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");
            String date = dateFormat.format(new Date());
            String fileName = "Kersplatt_" + date + ".jpg";
            output = new File(imagesFolder, fileName);
            try {
                FileOutputStream fos = new FileOutputStream(output);
                fos.write(data);
                fos.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
            camera.startPreview();
            }
};

}

在GameActivity中我有

PhotoTask photoTask = new PhotoTask(camera,surfaceCamera,isPreview,holder,GameActivity.this);
photoTask.execute(null);

触摸屏幕(拍照)

为什么我的方法onPictureTaken没有被调用? (当我删除onPostExecute方法时调用)

1 个答案:

答案 0 :(得分:2)

我建议采取

 Intent intent = new Intent(gameactivity.getApplicationContext(),ShareActivity.class);
 // Sending the picture taken to ShareActivity
 intent.putExtra("PICTURE_TAKEN", output);
 gameactivity.startActivity(intent);

到PictureCallback而不是onPostExecute

完成存储图像后,转到下一个活动