Camera.takePicture(...) not working on Device

All your problems with Audio, Video and Images.

Camera.takePicture(...) not working on Device

Postby caleb » Wed Mar 24, 2010 11:46 pm

Hi, I am making an app that takes a photo when the screen is touched. It runs perfectly in the emulator. However, when I test it out on my Droid (2.0.1), takePicture(shutterCallback, rawCallback, jpegCallback) does not run. I am 99% sure that the Camera is opened, as I can hear it start up (same sound as when default Camera app is started).

I do not get force closes or any other errors, the callbacks just aren't... called back.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.                 camera1 = Camera.open();
  3.  
  4.                 Camera.Parameters cParams = camera1.getParameters();
  5.  
  6.                 cParams.setPictureFormat(PixelFormat.JPEG);
  7.  
  8.                 camera1.setParameters(cParams);
  9.  
  10.  
  11.  
  12.                 linear.setOnTouchListener(new View.OnTouchListener() {
  13.  
  14.                         public boolean onTouch(View v, MotionEvent event) {
  15.  
  16.                                 //toaster("Touched scrn");
  17.  
  18.                                 camera1.takePicture(shutterCallback, rawCallback, jpegCallback);
  19.  
  20.                                 return false;
  21.  
  22.                         }
  23.  
  24.                 });
  25.  
  26.                 rawCallback = new PictureCallback() {
  27.  
  28.                         public void onPictureTaken(byte[] data, Camera cam) {
  29.  
  30.                                 toaster("rawcallback");
  31.  
  32.                         }
  33.  
  34.                 };
  35.  
  36.                 shutterCallback = new ShutterCallback(){
  37.  
  38.                         public void onShutter(){
  39.  
  40.                                 toaster("aw snap!");
  41.  
  42.                         }
  43.  
  44.                 };
  45.  
  46.         jpegCallback = new PictureCallback(){
  47.  
  48.                 public void onPictureTaken(byte[] _data, Camera _camera) {
  49.  
  50.                         toaster("jpg callback");
  51.  
  52.                         FileOutputStream outStream = null;
  53.  
  54.                         File external = Environment.getExternalStorageDirectory();
  55.  
  56.                         try {
  57.  
  58.                                 // write to sdcard
  59.  
  60.                                 outStream = new FileOutputStream(String.format(
  61.  
  62.                                                 external+"appname/%d.jpg", System.currentTimeMillis()));
  63.  
  64.                                 outStream.write(_data);
  65.  
  66.                                 outStream.flush();
  67.  
  68.                                 outStream.close();
  69.  
  70.                         } catch (FileNotFoundException e) {
  71.  
  72.                                 e.printStackTrace();
  73.  
  74.                         } catch (IOException e) {
  75.  
  76.                                 e.printStackTrace();
  77.  
  78.                         } finally {
  79.  
  80.                         }
  81.  
  82.                 }
  83.  
  84.         };
  85.  
  86.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


relevant items from Manifest:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2.     <uses-permission android:name="android.permission.CAMERA"/>
  3.  
  4.     <uses-feature android:name="android.hardware.camera"/>
  5.  
  6.     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  7.  
  8.  
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


toaster(String) just displays a toast (I use it to debug/follow through the program). toaster("Touched scrn") is successful. The other ones are not (meaning that after takePicture is called, the program does not make it to the callBacks)

Any help? Again, this is working in the emulator as expected but not on my phone (2.0.1 Droid, not rooted or anything like that).... Thanks for taking a look :)
caleb
Freshman
Freshman
 
Posts: 3
Joined: Tue Mar 23, 2010 7:09 pm

Top

Postby Boni2k » Fri Mar 26, 2010 9:40 am

Does the "Camera.java"-Demo provided by the SDK work for you?
Or that one: http://snippets.dzone.com/posts/show/8683 ?
Boni2k
Experienced Developer
Experienced Developer
 
Posts: 88
Joined: Sun Dec 20, 2009 9:46 pm

Re:

Postby Kai » Wed Dec 22, 2010 5:21 pm

Boni2k wrote:Does the "Camera.java"-Demo provided by the SDK work for you?
Or that one: http://snippets.dzone.com/posts/show/8683 ?


Sorry for reviving this thread, but it's the one I found using Google.

I have exactly the same problem, using Android 2.2.1 on an Archos.
My program works fine in the emulator but not on the device.

Are there any solutions/workarounds?


Thanks!
Kai
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Dec 22, 2010 4:16 pm

Re: Camera.takePicture(...) not working on Device

Postby suero » Wed Dec 22, 2010 6:31 pm

I am also with this problem


....some solution ?
suero
Freshman
Freshman
 
Posts: 3
Joined: Tue Dec 21, 2010 2:56 pm

Re: Camera.takePicture(...) not working on Device

Postby julienr » Sun Feb 20, 2011 11:26 pm

I've had this problem and solved it by doing the following :
- You have to be previewing (called startPreview() on a valid surface) before you take a picture
- You shouldn't stop the preview before taking the picture, it will do it automatically. But if you want to take a new picture, you have to call startPreview again

I would recommend looking at the source for the stock Camera application :
http://android.git.kernel.org/?p=platfo ... ads/master
julienr
Junior Developer
Junior Developer
 
Posts: 19
Joined: Fri Mar 12, 2010 4:05 pm

Re: Camera.takePicture(...) not working on Device

Postby strider2023 » Wed Feb 23, 2011 1:48 pm

Hi caleb,

You can try this following code. The function below is for taking picture.

Code: Select all
/**
     * This function calls the native camera application to launch which saves the picture to a pre-defined path.
     */
    private void onTakePic() {
      Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
      //PRE_DEF and EXTESION_PIC are just pre-defined strings. imageStraoge_File is an object of the file class defining the path where it will save the picture.
      file = new File (imageStorage_File, PRE_DEF + String.valueOf(System.currentTimeMillis()) + EXTENSION_PIC);
      intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
     
      startActivityForResult(intent, TAKE_PICTURE);     
    }


Please do add
Code: Select all
<uses-permission android:name="android.permission.CAMERA" />
   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
strider2023
Senior Developer
Senior Developer
 
Posts: 126
Joined: Tue Mar 02, 2010 6:34 am
Location: Chennai, India

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 33 guests