Android take picture

When you build an Android application that allows the user to take picture, this tip might be useful to you. In this Android tip, I am going to show you how to launch Android's Camera application from your application and after the picture is taken, it returns back to your application with the captured picture to display in the ImageView.

To launch the Camera application, you need to create an Intent object for image capture action. Then call the startActivityForResult() method to open the Camera application. You will pass the Intent object and a request code. To receive the captured picture, you need to add the onActivityResult() method to the activity that launches the Camera application. The onActivityResult() has three arguments: request code, result code, and an Intent object.



Now to have an example application on taking picture in Android, you create a new Android project. Then modify the activity_main.xml file to add one ImageView and one Button. The ImageView displays the captured image and the Button is clicked to launch the Camera application.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context=".MainActivity"

   >
  
   <ImageView
      android:id="@+id/imgview"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentTop="true"
      android:layout_above="@+id/bt1"
       />
   <Button
     android:id="@+id/bt1"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="Launch Camera"
     android:onClick="launchCamera"
     android:layout_alignParentBottom="true"
     android:layout_centerHorizontal="true"
    
     />
  
</RelativeLayout>



In the MainActivity class, you need to add two methods: launchCamera() and onActivityResult(). Then launchCamera() method is invoked to launch the Camera application when you click the Launch Camera button. In the onActivityResult() method, you receive the captured picture and display it in the ImageView.


package com.example.androidexample;

import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends Activity {
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 10;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

}

public void launchCamera(View view)
{
// create Intent to take a picture and return control to the calling application
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//start the image capture Intent
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
//display the captured image in ImageView
ImageView imgView=(ImageView)findViewById(R.id.imgview);
     Uri imgUri=data.getData();
     imgView.setImageURI(imgUri);


} else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
} else {
// Image capture failed
}
}
}

public boolean onCreateOptionsMenu(Menu menu) {
//Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}




Android-take-picture

Posted by: Dara | post date: 09-07-2014 | Subject: Android Apps Development

write programming tip



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.