I have to launch the camera, and when the users has done the picture, I have to take it and show it in a view.
Looking at http://developer.android.com/guide/topics/media/camera.html I have done:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bLaunchCamera = (Button) findViewById(R.id.launchCamera);
bLaunchCamera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "lanzando camara");
//create intent to launch camera
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
imageUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); //create a file to save the image
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); //set the image file name
//start camera
startActivityForResult(intent, CAMERA_REQUEST);
}
});
/**
* Create a File Uri for saving image (can be sued to save video to)
**/
private Uri getOutputMediaFileUri(int mediaTypeImage) {
return Uri.fromFile(getOutputMediaFile(mediaTypeImage));
}
/**
* Create a File for saving image (can be sued to save video to)
**/
private File getOutputMediaFile(int mediaType) {
//To be safe, is necessary to check if SDCard is mounted
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
(String) getResources().getText(R.string.app_name));
//create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d(TAG, "failed to create directory");
return null;
}
}
//Create a media file name
String timeStamp = new SimpleDateFormat("ddMMyyyy_HHmmss").format(new Date());
File mediaFile;
if (mediaType == MEDIA_TYPE_IMAGE) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
} else {
return null;
}
return mediaFile;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == CAMERA_REQUEST) {
if(resultCode == RESULT_OK) {
//Image captured and saved to fileUri specified in Intent
Toast.makeText(this, "image saved to:
" + data.getData(), Toast.LENGTH_LONG).show();
Log.d(TAG, "lanzando camara");
} else if(resultCode == RESULT_CANCELED) {
//user cancelled the image capture;
Log.d(TAG, "usuario a cancelado la captura");
} else {
//image capture failed, advise user;
Log.d(TAG, "algo a fallado");
}
}
}
When the picture has been done, the app crashes when it try to send the 'Toast' info because 'data' is null.
But if I debug the app I can see that the image has been saved.
So my question is: How can I get the path in the onActivityResult?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…