我想与活动“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 方法时调用)