4

I have developed an application which captures a photo when punch in at the time. It's working good on Acer tab(capturing image and saves in sdcard). Now when I run the same application in Samsung Galaxy(Android-4.1.1) My app is getting 'Unfortunately app has stopped' when click on punch in. and my code is here goes..

// ClockIn functionality
clockin_btn.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {       
        clockin_btn.setEnabled(false);
        camera.stopPreview();
        capturePhoto(0);
      // showing one error here in log cat
        previewing = false;         
        clockin_label.setText(String.format(session_msg,logout_seconds));   
        ticker.setBase(SystemClock.elapsedRealtime()); 
        ticker.start();
    }       
});

private String capturePhoto(int clockInOutMode) {

    final int mode = clockInOutMode;
    img_file = String.format("%d.jpg", System.currentTimeMillis());

    Camera.PictureCallback mCall = new Camera.PictureCallback() {               

        public void onPictureTaken(byte[] data, Camera camera) {

            try {    

                Bitmap mBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);           
                File file = new File(Constants.EMP_IMG_LOCATION, img_file);           
                FileOutputStream fOut = new FileOutputStream(file);                    
                mBitmap.compress(Bitmap.CompressFormat.JPEG, 80, fOut);               
                fOut.flush();               
                fOut.close();

                if ( mode == 0) {
                    clockIn(img_file);
                } else {
                    clockOut(img_file);
                }

            } catch (FileNotFoundException e) {
                e.printStackTrace();

            } catch (IOException e) {        
                e.printStackTrace();

            } 
        }   

    };  

    Camera.Parameters cameraParams = camera.getParameters();
    List<Camera.Size> sizes = cameraParams.getSupportedPictureSizes();
    Camera.Size result = null;
    for (int i=0;i<sizes.size();i++){
        result = (Size) sizes.get(i);
        Log.i("PictureSize", "Supported Size.Width: " + result.width + "height: " +result.height);         
    }

    cameraParams.setPictureSize(640, 480);
    camera.setParameters(cameraParams);
            System.gc();
    camera.setPreviewCallback(null);
    camera.takePicture(null, null, mCall);

       // showing one error here in logcat

    return img_file;
}

My Logcat is showing as:

03-27 04:52:19.273: E/AndroidRuntime(4105): FATAL EXCEPTION: main
03-27 04:52:19.273: E/AndroidRuntime(4105): java.lang.RuntimeException: setParameters failed
03-27 04:52:19.273: E/AndroidRuntime(4105):     at android.hardware.Camera.native_setParameters(Native Method)
03-27 04:52:19.273: E/AndroidRuntime(4105):     at android.hardware.Camera.setParameters(Camera.java:1452)

and my android.manifest.xml file:

     <uses-feature android:name="android.hardware.camera" />
 <uses-feature android:name="android.hardware.camera.autofocus" />
 <used-feature android:name="android.hardware.location" />
 <used-feature android:name="android.hardware.camera.setParameters" />
richsage
  • 26,912
  • 8
  • 58
  • 65
kumar Sudheer
  • 705
  • 3
  • 8
  • 28
  • i u comment setParameters then app working or not? – ρяσѕρєя K Mar 27 '13 at 06:10
  • @ρяσѕρєяK No, Its not working. now the error is showing on next line i.e camera.takePicture(null, null, mCall); – kumar Sudheer Mar 27 '13 at 06:47
  • @rajpara, I am also facing the same issue. can you help me how to solve the issue based on my code.. – kumar Sudheer Mar 28 '13 at 03:55
  • 1
    System.gc(); - don't do that please! Its a n00b attempt and it doesn't solve anything. It only hints the JVM that now is a good time to do GC - but in the end its the JVM that makes the choice wether to GC or not. – slott Jul 23 '15 at 09:09

3 Answers3

2

Call the startPreview method before you call takePicture method camera.takePicture(null, null, mCall); and the startPreview method I used is

private void startPreview() {
        if (cameraConfigured && camera!=null) {
          camera.startPreview();
          inPreview=true;
        }
      }

and through this I solved my issue... It may help you guys.

Cathy
  • 377
  • 1
  • 4
  • 21
0

It is not working in all cases. You call getSupportedPictureSizes() then you can get the list. And choose parameters in setPictureSize() from the list.

Sohyun Ahn
  • 240
  • 2
  • 9
0
private void flipBackToFrontCamera() {

    if (mCamera != null) {
        mCamera.stopPreview();
        mCamera.release();
        mCamera = null;
    }

    mCamera = Camera.open(1);

    if (mCamera != null) {
        try {
            mCamera.setPreviewDisplay(surfaceView.getHolder());
            mCamera.startPreview();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Krishna
  • 1,556
  • 13
  • 21