Simple code to capture an image by camera from android powered devices...

    In this application by pressing a button,we will open the camera preview window. After that we will capture an image and save it to gallery of the device.So here comes the simple xml layout design for this. You can always make your layout design as per your need.

?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Capture Image" />

    <Button
        android:id="@+id/btCamera_main"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Take Picture" />

</LinearLayout>
Now here is the activity class(CameraTestAppsActivity.java) that makes the button working...

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class CameraTestAppsActivity extends Activity {

Button btCamera;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btCamera=(Button)findViewById(R.id.btCamera_main);
              
        btCamera.setOnClickListener(new OnClickListener()
        {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
     Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
     startActivityForResult(intent, 0);

}
});
         
    }
   
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK && requestCode == 0) {
                  String result = data.toURI();
    }
    }
}
In this code by tapping the button we call the camera intent class via this code snippet ...
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
then we use startActivityForResult(intent, 0); to get the result code when image capturing is complete. As a last step please add the following code snippet to your Android manifest.
<uses-permission android:name="android.permission.CAMERA"></uses-permission>

1 comment:

  1. I was recommended this website by my cousin. I'm not sure whether this
    post is written by him as nobody else knpw such detailed about my trouble.
    Yoou aree incredible! Thanks!

    Feel free to visit my homepage would, ,

    ReplyDelete