I have an activity A in which i have a button and an image view. When i click the button it should load the camera and save the image in the image view.Can anyone provide code for this.
Thanks in Advance !!!
I have an activity A in which i have a button and an image view. When i click the button it should load the camera and save the image in the image view.Can anyone provide code for this.
Thanks in Advance !!!
see below code.. it may help you.
put this two line in your button click..
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent, 0);
create this method in you code..
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0 && resultCode == RESULT_OK) {
if (data != null) {
photo = (Bitmap) data.getExtras().get("data");
add_image.setImageBitmap(photo); /* this is image view where you want to set image*/
Log.d("camera ---- > ", "" + data.getExtras().get("data"));
}
}
}
...........................................................................................
below code give you LastImageID taken by camera. this is just extra code you want hole image then its use-full to you.
........................................................................................
private String getLastImageId() {
final String[] imageColumns = { MediaStore.Images.Media._ID,
MediaStore.Images.Media.DATA };
final String imageOrderBy = MediaStore.Images.Media._ID + " DESC";
Cursor imageCursor = managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, imageColumns,
null, null, imageOrderBy);
if (imageCursor.moveToFirst()) {
int id = imageCursor.getInt(imageCursor
.getColumnIndex(MediaStore.Images.Media._ID));
String fullPath = imageCursor.getString(imageCursor
.getColumnIndex(MediaStore.Images.Media.DATA));
imageCursor.close();
return fullPath;
} else {
return "no path";
}
}
You can create a method to capture the image and call it in the onClickListener of your button, like this:
public void takePicture(View view){
try{
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
} catch(Exception e){
e.printStackTrace();
}
}
And then override onActivityResult method like this:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
case CAMERA_PIC_REQUEST:
String path=null;
try{
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
thumbnail = Bitmap.createScaledBitmap(thumbnail,yourDesiredImageWidth, yourDesiredImageHeight, true);
ImageView imageView = (ImageView) findViewById(R.id.yourImageView);
image.setImageBitmap(thumbnail);
break;
}catch(Exception ee){
ee.printStackTrace();
}
}
}
Try as:
STEP 1: Start the camera for taking an image:
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 2);
STEP 2: In onActivityResult
get Image from data
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
ImageView imageView=(ImageView) findViewbyId(R.id.imgv);
imageView.setImageBitmap(photo);
}
EDIT : and second solution:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "temp.jpg")));
startActivityForResult(intent, 2);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == NONE)
return;
if (requestCode == 2) {
File picture = new File(Environment.getExternalStorageDirectory() + "/temp.jpg");
ImageView imgView=(ImageView)findViewById(R.id.imgView);
Uri imgUri=Uri.fromFile(picture);
imageView.setImageURI(imgUri);
}
}
Your Activity code
public class MainActivity extends Activity {
/** Called when the activity is first created. */
Button btnTakePhoto;
ImageView imgTakenPhoto;
private static final int CAM_REQUREST = 1313;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnTakePhoto = (Button) findViewById(R.id.button1);
imgTakenPhoto = (ImageView) findViewById(R.id.imageView1);
btnTakePhoto.setOnClickListener(new btnTakePhotoClicker());
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_PIC_REQUEST && resultCode == RESULT_OK) {
if (data != null) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
imgTakenPhoto.setImageBitmap(thumbnail);
}
}
}
class btnTakePhotoClicker implements Button.OnClickListener
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
}
}
Your Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Press the button, capture photo and enjoy!!"
android:id="@+id/textview1"/>
<Button
android:text="Take Photo!"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textview1">
</Button>
<ImageView
android:id="@+id/imageView1"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:src="@drawable/icon"
android:scaleType="centerCrop"
android:layout_below="@+id/button1">
</ImageView>
</RelativeLayout>
CemraImageActivity.java
public class CemraImageActivity extends Activity implements OnClickListener{
private int CAMERA_REQUEST;
private ImageView imageView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.imageView = (ImageView)this.findViewById(R.id.image_capture);
imageView.setOnClickListener(this);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode != 0)
{
if (requestCode == CAMERA_REQUEST)
{
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
}
public void onClick(View v) {
// TODO Auto-generated method stub
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
}
main.xml
<?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:id="@+id/textView1"
android:layout_width="334dp"
android:layout_height="wrap_content"
android:text="Click this image for take a photo"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="@+id/image_capture"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.06"
android:src="@drawable/ic_launcher" />
</LinearLayout>
very simple way to get a Click button and set store image in imageview.
`public class MainActivity extends Activity {
private static final int CAMERA_PIC_REQUEST = 22;
Uri cameraUri;
Button BtnSelectImage;
private ImageView ImgPhoto;
private String Camerapath ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImgPhoto = (ImageView) findViewById(R.id.imageView1);
BtnSelectImage = (Button) findViewById(R.id.button1);
BtnSelectImage.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
try {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Couldn't load photo", Toast.LENGTH_LONG).show();
}
}
});
}
@Override
public void onActivityResult(final int requestCode, int resultCode, Intent data) {
try {
switch (requestCode) {
case CAMERA_PIC_REQUEST:
if (resultCode == RESULT_OK) {
try {
Bitmap photo = (Bitmap) data.getExtras().get("data");
ImgPhoto.setImageBitmap(photo);
} catch (Exception e) {
Toast.makeText(this, "Couldn't load photo", Toast.LENGTH_LONG).show();
}
}
break;
default:
break;
}
} catch (Exception e) {
}
}
}`
Set this Manifest.
<uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" android:required="false"></uses-feature>
fist you need in manifest do this after in mainactivity.java do this
public class MainActivity extends Activity {
Button button;
ImageView photo;
static final int Cam_Request = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.btnPhoto);
photo = (ImageView)findViewById(R.id.ivPhoto);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent camera_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = getFile();
camera_intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
startActivityForResult(camera_intent, Cam_Request);
}
});
}
private File getFile ()
{
File folder = new File("sdcard/camera_app");
if(!folder.exists())
{
folder.mkdir();
}
File image = new File(folder,"image.jpg");
return image;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
String path = "sdcard/camera_app/image.jpg";
photo.setImageDrawable(Drawable.createFromPath(path));
}
}