1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
/******* PRENDRE PHOTO ********/
Button buttonTakePicture = (Button) findViewById(R.id.buttontakePicture);
buttonTakePicture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getTempFile(MainActivity.this)) );
startActivityForResult(takePictureIntent, RESULT_TAKE_PHOTO);
}
});
/****** MA METHODE onActivityResult() *******/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
preview = (ImageView) findViewById(R.id.imgView);
if (requestCode == RESULT_TAKE_PHOTO && resultCode == RESULT_OK) {
//Bundle extras = data.getExtras();
// mImageBitmap = (Bitmap) extras.get("data");
//preview.setImageBitmap(mImageBitmap);
final File file = getTempFile(this);
try {
mImageBitmap = Media.getBitmap(getContentResolver(), Uri.fromFile(file) );
preview.setImageBitmap(mImageBitmap);
// do whatever you want with the bitmap (Resize, Rename, Add To Gallery, etc)
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/*** MA METHODE getTempFile qui récupère le fichier temporaire ***/
private File getTempFile(Context context){
//it will return /sdcard/image.tmp
final File path = new File( Environment.getExternalStorageDirectory(), context.getPackageName() );
if(!path.exists()){
path.mkdir();
}
return new File(path, "image.tmp");
} |
Partager