Android : Method onPictureTaken never called, even with AsyncTask
I want to
With the activity “GameActivity”
Take a picture
Save the picture on SD card
Use the picture with another activity
The problem is, when I just save the picture and do nothing with it, the picture is saved. When I start the activity (method startActivity) just after camera.takePicture, the method onPictureTaken is never called.
I’ve read it was because the method takePicture is long to execute so I’ve followed the advice on this site (http://www.workreloaded.com/2011/06/how-to-use-the-android-camera/) and created a class that extends 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(); } };
}
While in GameActivity I have
PhotoTask photoTask = new PhotoTask(camera,surfaceCamera,isPreview,holder,GameActivity.this); photoTask.execute(null);
when the screen is touched (to take a photo)
Why does my method onPictureTaken is not called there? (yet called when I delete onPostExecute method)
Answer
i would recommend taking
Intent intent = new Intent(gameactivity.getApplicationContext(),ShareActivity.class); // Sending the picture taken to ShareActivity intent.putExtra("PICTURE_TAKEN", output); gameactivity.startActivity(intent);
to PictureCallback instead of onPostExecute
when you are done with storing image then move to next activity
Filed under: Android Coding FAQ
Tags:
Comments
No Comments
Leave a reply
You must be logged in to post a comment.