1.调用摄像头
takePhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, TAKE_PHOTO);
}
});
2.Bitmap转File
public File saveBitmapFile(Bitmap bitmap) {
// File file = new File(getExternalCacheDir(), "temp.jpg");//将要保存图片的路径
File file = new File(getExternalCacheDir(), "temp.jpg");//将要保存图片的路径
try {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
// bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
bos.flush();
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
return file;
}
3.获取结果
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data != null) {
if (requestCode == TAKE_PHOTO && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
picture.setImageBitmap(photo);
File file = saveBitmapFile(photo);
String absolutePath = file.getAbsolutePath();
Log.d(TAG, "onActivityResult: " + absolutePath);
}
}
}