Hi.
Once again, this is Bogdan with another article for androidgenuine.com.
Today I will show you a simple but very usefull thing. I will show you how to start anactivityforresult to the camera and how the taken photo will be transformed into a bitmap a seen on an imageview.
The tutorial is simple and pretty short. I have only one class : ImageCapture.class and an xml file : mylayout.xml (like usual, I am too lazy to change the name of the layout).
Here is the Java code for ImageCapture class:
public class ImageCapture extends Activity implements View.OnClickListener{ //The declaration part... Button b; ImageView iv; final static int cameraData = 0; Bitmap bmp; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.mylayout); //We call for initialize method initialize(); //We use the inputstream to get the drawable resource InputStream is = getResources().openRawResource(R.drawable.ic_launcher); bmp = BitmapFactory.decodeStream(is); } private void initialize() { // TODO Auto-generated method stub //Stuff with imageview and button, I suppose you know these. iv = (ImageView) findViewById(R.id.ivReturnedPic); b = (Button) findViewById (R.id.bTakePic); b.setOnClickListener(this); } public void onClick(View v) { // TODO Auto-generated method stub //Here is the intent that we use to start the Camera Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); //Here we start the activity for result with the unique code "cameraData" and intent i. startActivityForResult(i, cameraData); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); //Check the unique code if it matches with the RESULT_OK if (resultCode == RESULT_OK){ //The bundle where we collect the extras Bundle extras = data.getExtras(); // Here is our bitmap - the photo we'd just taken bmp = (Bitmap) extras.get("data"); //Here we set the bitmap to our imageview iv.setImageBitmap(bmp); } } }
And here is the code for the xml file:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ImageView android:id="@+id/ivReturnedPic" android:layout_width="250dp" android:layout_height="250dp" android:layout_gravity="center" android:layout_marginTop="20dip" android:src="@drawable/ic_launcher" /> <Button android:layout_gravity="center" android:id="@+id/bTakePic" android:layout_width="150dp" android:layout_marginTop="10dip" android:layout_height="wrap_content" android:text="Take picture" /> </LinearLayout>
Here is a picture where you can see how the application looks like :
Like always, you can download the entire project from here.
This was all for this tutorial . I hope you enjoyed it and you’ll read my future articles , too. Until next tutorial, bye !



