• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

capture an image through camara

final String [] items = new String [] {"From Camera", "From SD Card","Exit"};
ArrayAdapter<String> adapter = new ArrayAdapter<String> (this, android.R.layout.select_dialog_item,items);
AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setTitle("Select Image");
builder.setAdapter( adapter, new DialogInterface.OnClickListener() {
public void onClick( DialogInterface dialog, int item ) {
if (item == 0) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment.getExternalStorageDirectory(),
"tmp_avatar_" + String.valueOf(System.currentTimeMillis()) + ".jpg");
mImageCaptureUri = Uri.fromFile(file);

try {
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
intent.putExtra("return-data", true);
startActivityForResult(intent, PICK_FROM_CAMERA);
} catch (Exception e) {
e.printStackTrace();
}

dialog.cancel();
} else if(item==1){
Intent intent = new Intent();

intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);

startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_FILE);
}
else{
dialog.cancel();
ChooseAPic.this.finish();
}
}
}




/////////////////////////////////////////////////////And for getting///////////

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK) return;

Bitmap bitmap = null;
String path = "";

if (requestCode == PICK_FROM_FILE) {
mImageCaptureUri = data.getData();
path = getRealPathFromURI(mImageCaptureUri); //from Gallery

if (path == null)
path = mImageCaptureUri.getPath(); //from File Manager

if (path != null)
bitmap = BitmapFactory.decodeFile(path);
} else {
path = mImageCaptureUri.getPath();
bitmap = BitmapFactory.decodeFile(path);
}

//bmp=bitmap;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
//bitmap.compress(Bitmap.CompressFormat.PNG, 90, bos); //compress to which format you want.
//bitmapdata = bos.toByteArray();
//bitmapdata = bos.toByteArray();
bitmapdata=path;
mImageView.setImageBitmap(bitmap);

//bitmapdata=bitmap;

}


:)
 
Back
Top Bottom