我已经使用内置图像捕获活动创建了拍照活动,但仍然无法将图片存储在 sdcard 中并查看捕获的图像。Intent 已启动,我可以拍照但当我单击确定(保存)时,什么也没有发生。以下是我的活动:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.picturelayout);
imageForUpload = (ImageView) findViewById(R.id.trackMePicture);
btnBack = (Button) findViewById(R.id.btnBack);
intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, getImageUri(this));
startActivityForResult(intent, TAKE_PHOTO_CODE);
}
/**
* @return
*/
private Uri getImageUri(Context context) {
// TODO Auto-generated method stub
File file =newFile(Environment.getExternalStorageDirectory(),context.getPackageName());
if(!file.exists())
file.mkdir();
File newFile=new File(file,new Date().toString()+".jpg");
Uri imagePath=Uri.fromFile(newFile);
return imagePath;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==TAKE_PHOTO_CODE ){
if(resultCode==-1){
Toast.makeText(getApplicationContext(), "Result code : "+resultCode, Toast.LENGTH_LONG).show();
//Uri imagePath=getImageUri();
Bitmap b;
try {
b = Media.getBitmap(getContentResolver(), getImageUri(this));
imageForUpload.setImageBitmap(b);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
Toast.makeText(getApplicationContext(), "Result code : "+resultCode, Toast.LENGTH_LONG).show();
}
}
else{
Toast.makeText(getApplicationContext(), "Request code : "+requestCode, Toast.LENGTH_LONG).show();
}
}