7

I want to use camera facility in android application.

I want to capture image on click of button control can any one suggest me the best example of it.

Freni
  • 151
  • 1
  • 3
  • 17

6 Answers6

6

Try android dev's site: Camera access in android also read this article: Using android camera

And for more look at related section of this question.

enter image description here

Harry Joy
  • 58,650
  • 30
  • 162
  • 207
2

Please Check this answer :

public class ImageUploading extends Activity 
{   
    Uri imageUri = null;
    ImageButton btnSubmit   ;
    public void onCreate(Bundle onsavedInstantState)
    {
        super.onCreate(onsavedInstantState);
        setContentView(R.layout.edit_profile);
        btnSubmit               = (ImageButton) findViewById(R.id.btnSubmit);       
        btnSubmit.setClickable(true);       
        btnSubmit.setOnClickListener(new View.OnClickListener()
        {           
            @Override
            public void onClick(View v) {           
                Intent cameraIntent = new Intent("android.media.action.IMAGE_CAPTURE");
                File imgFile = new File(Environment.getExternalStorageDirectory(),"my_photo.png");
                imageUri = Uri.fromFile(imgFile);
                cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);               
                startActivityForResult(cameraIntent,0);             
            }
        });
    }   
    @Override
    public void onActivityResult(int RequestCode, int ResultCode, Intent imageIntent)
    {
        super.onActivityResult(RequestCode, ResultCode, imageIntent);
        try
        {
            if(RequestCode == 0)
            {
                if(ResultCode == Activity.RESULT_OK)
                {
                    getContentResolver().notifyChange(imageUri, null);
                    ContentResolver objContentResolver = getContentResolver();
                    Bitmap imgBitmap = android.provider.MediaStore.Images.Media.getBitmap(objContentResolver, imageUri);
                    Drawable imgDrawable = new BitmapDrawable(imgBitmap);
                    btnSubmit.setBackgroundDrawable(imgDrawable);
                }
            }           
        }
        catch(Exception e)
        {}
    }
}

It will capture image on button click, and set this image as background image for that button itself.

Jomia
  • 3,414
  • 10
  • 46
  • 63
2
@Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button button = (Button)findViewById(id);
 button.setonClickListener(new View.onClickListener()
            {
         public void onClick(View view)
            {
                Intent intent = new Intent();
                intent.putExtra("aspectX", 730);
                intent.putExtra("aspectY", 1115);
                intent.putExtra("outputX", 730);
                intent.putExtra("outputY", 1115);
                intent.setAction("android.media.action.IMAGE_CAPTURE");
           startActivityForResult(intent, PICK_FROM_CAMERA);
                }
        @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
        super.onActivityResult(requestCode, resultCode, data);

        switch(requestCode)
        {
            case PICK_FROM_CAMERA : if (resultCode == RESULT_OK)
            { 
                ContentValues values = new ContentValues();
                values.put(Images.Media.TITLE, "title");
                values.put(Images.Media.BUCKET_ID, "test");
                values.put(Images.Media.DESCRIPTION, "test Image taken");
                values.put(Images.Media.MIME_TYPE, "image/jpeg");
                Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
                Bitmap photo = (Bitmap) data.getExtras().get("data");
                ((ImageView)findViewById(R.id.selectedimage)).setImageBitmap(photo);
                OutputStream outstream;
                try {
                        outstream = getContentResolver().openOutputStream(uri);
                        photo.compress(Bitmap.CompressFormat.JPEG,100, outstream);
                        outstream.close();
                } catch (FileNotFoundException e) {}
                catch (IOException e){}
            }
            break;

EDIT: its perfect and working.just try it and provide me feedback if u like it.

Geetanjali
  • 1,043
  • 3
  • 13
  • 37
1

Try this one, if it doesnt work I will give you the trunk link.

http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/CameraPreview.html

Anatoliy Nikolaev
  • 22,370
  • 15
  • 69
  • 68
Max
  • 5,380
  • 6
  • 42
  • 66