gpt4 book ai didi

Android : Method onPictureTaken never called, 即使使用 AsyncTask

转载 作者:行者123 更新时间:2023-11-29 21:50:15 26 4
gpt4 key购买 nike

我要随着 Activity “GameActivity”拍照将图片保存在 SD 卡上将图片用于其他 Activity

问题是,当我只是保存图片而不对其进行任何操作时,图片已保存。当我在 camera.takePicture 之后启动 Activity (方法 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 方法时调用)

最佳答案

我会推荐

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

到 PictureCallback 而不是 onPostExecute

当你完成存储图像然后移动到下一个 Activity

关于Android : Method onPictureTaken never called, 即使使用 AsyncTask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14605592/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com