0 Replies - 573 Views - Last Post: 01 April 2013 - 01:30 PM

#1 dhwanishah  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 01-April 13

Problem with camera code for my android app?!? Any ideas?

Posted 01 April 2013 - 01:30 PM

Hi I am trying to take a picture in android when a user clicks the button. Now this is using the internal camera, but the user doesn't see what he is taking picture of. It just takes a picture of whatever the camera sees on the front when button pressed. For some reason this code is not working. Any ideas?


Code:
    package com.example.servicetest4;

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
 
    import android.os.Bundle;
    import android.os.Environment;
    import android.app.Activity;
    import android.hardware.Camera;
    import android.hardware.Camera.PictureCallback;
    import android.hardware.Camera.ShutterCallback;
    import android.util.Log;
    import android.view.View;
    import android.view.View.onclickListener;
    import android.widget.Button;
    import android.widget.Toast;

    public class MainActivity extends Activity implements onclickListener {
	
	public static final int MEDIA_TYPE_IMAGE = 1;
	public static final String TAG = "TAG";
    private Camera mCamera;
    ShutterCallback sc;
    //private CameraPreview mPreview;
    
    private PictureCallback mPicture = new PictureCallback() {
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
            if (pictureFile == null){
                Log.d(TAG, "Error creating media file, check storage permissions: ");
                return;
            }
            try {
                FileOutputStream fos = new FileOutputStream(pictureFile);
                fos.write(data);
                fos.close();
            } catch (FileNotFoundException e) {
                Log.d(TAG, "File not found: " + e.getMessage());
            } catch (IOException e) {
                Log.d(TAG, "Error accessing file: " + e.getMessage());
            }
        }
    };
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ((Button) findViewById(R.id.snap)).setonclickListener(this);
        mCamera = getCameraInstance();
        //mCamera.takePicture(sc, null, null);
    }

	@Override
	public void onclick(View v) {
		if (v.getId()== R.id.snap) {
			//mCamera.takePicture(sc, null, null);
			mCamera.takePicture(null, null, mPicture);
			Toast.makeText(this, "Pic taken", Toast.LENGTH_SHORT).show();
		}		
	}
    
	/** A safe way to get an instance of the Camera object. */
	public static Camera getCameraInstance(){
	    Camera c = null;
	    try {
	        c = Camera.open(); // attempt to get a Camera instance
	    }
	    catch (Exception e){
	        // Camera is not available (in use or does not exist)
	    }
	    return c; // returns null if camera is unavailable
	}
    
	/** Create a File for saving an image or video */
	private static File getOutputMediaFile(int type){
	    /*** To be safe, you should check that the SDCard is mounted
	    // using Environment.getExternalStorageState() before doing this.
	    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
	              Environment.DIRECTORY_PICTURES), "MyCameraApp");
	    // This location works best if you want the created images to be shared
	    // between applications and persist after your app has been uninstalled.
	    // Create the storage directory if it does not exist
	    if (! mediaStorageDir.exists()){
	        if (! mediaStorageDir.mkdirs()){
	            Log.d("MyCameraApp", "failed to create directory");
	            return null;
	        }
	    }***/
	    // Create a media file name
	    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
	    File mediaFile;
	    if (type == MEDIA_TYPE_IMAGE){
	        mediaFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/external_sd/uoflsli/pics"
	        					+ File.separator + "IMG_"+ timeStamp + ".jpg");
	    } else {
	        return null;
	    }
	    return mediaFile;

	}
}


Any help appreciated! Thanks!

Is This A Good Question/Topic? 0
  • +

Page 1 of 1