open camera using intent
File destination;
Uri selectedImage;
public static String selectedPath1 = "NONE";
private static final int PICK_Camera_IMAGE = 2;
private static final int SELECT_FILE1 = 1;
public static Bitmap bmpScale;
public static String imagePath;
mcamera = (Button) findViewById(R.id.button1);
mcamera.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String name = dateToString(new Date(), "yyyy-MM-dd-hh-mm-ss");
destination = new File(Environment
.getExternalStorageDirectory(), name + ".jpg");
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(destination));
startActivityForResult(intent, PICK_Camera_IMAGE);
}
});
// ......................gallery_function..........//
mgallery = (Button) findViewById(R.id.button2);
mgallery.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
openGallery(SELECT_FILE1);
}
});
for intent // .........................Gallery function.................//
public void openGallery(int SELECT_FILE1) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(
Intent.createChooser(intent, "Select file to upload "),
SELECT_FILE1);
}
//............ intent .........
// ..................
protected void onActivityResult(int requestCode, int resultCode,
Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
Uri selectedImageUri = null;
String filePath = null;
switch (requestCode) {
case SELECT_FILE1:
if (resultCode == Activity.RESULT_OK) {
selectedImage = imageReturnedIntent.getData();
if (requestCode == SELECT_FILE1) {
selectedPath1 = getPath(selectedImage);
// mimagepath.setText(selectedPath1);
// Toast.makeText(Camera.this, "" + selectedPath1 + "",
// 500).show();
if (selectedPath1 != null) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
// image path `String` where your image is located
BitmapFactory.decodeFile(selectedPath1, options);
// Log.d("setpath ", "setpath " + selectedPath1);
;
}
}
break;
case PICK_Camera_IMAGE:
if (resultCode == RESULT_OK) {
try {
in = new FileInputStream(destination);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
imagePath = destination.getAbsolutePath();
// Toast.makeText(Camera.this, "" + imagePath +
// "",Toast.LENGTH_LONG).show();
break;
}
}
try this code
public static final int CAMERA_PIC_REQUEST = 1;//firstly define this
Intent photo= new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(photo, CAMERA_PIC_REQUEST);
The parameter passed to an "on-click" method is of type View
not int
as you show.
According to this tutorial your onClick method should be as follows:
public void onclick( View v ){
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePictureIntent, CAMERA_PIC_REQUEST);
}
Where CAMERA_PIC_REQUEST
is defined as (although I'm not quite sure why you would need to statically hard-code this value in your application):
private static final int CAMERA_PIC_REQUEST = 1337;
Update
CAMERA_PIC_REQUEST
is used to uniquely identify the result being returned to onActivityResult
. Multiple startActivityForResult
requests could be outstanding at one time.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_PIC_REQUEST) {
if (resultCode == RESULT_OK) {
tv.setText("Got picture!");
imageData = (Bitmap) data.getExtras().get("data");
ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageBitmap(imageData);
} else if (resultCode == RESULT_CANCELED){
tv.setText("Cancelled");
}
}
}