问题描述
我必须启动相机,当用户完成照片后,我必须将其拍摄并在视图中显示.
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.
查看 http://developer.android.com/guide/topics/media/camera.html 我已经完成:
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:\n" + 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");
}
}
}
图片处理完毕后,由于数据"为null,因此当应用程序尝试发送"Toast"信息时会崩溃.
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.
所以我的问题是:如何在onActivityResult中获取路径?
So my question is: How can I get the path in the onActivityResult?
推荐答案
以下是我用于捕获和保存摄像机图像,然后将其显示到imageview的代码.您可以根据需要使用.
Here is code I have used for Capturing and Saving Camera Image then display it to imageview. You can use according to your need.
您必须将摄像机"图像保存到特定位置,然后从该位置获取然后将其转换为字节数组.
You have to save Camera image to specific location then fetch from that location then convert it to byte-array.
这是打开捕获摄像机图像活动的方法.
Here is method for opening capturing camera image activity.
private static final int CAMERA_PHOTO = 111;
private Uri imageToUploadUri;
private void captureCameraImage() {
Intent chooserIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File f = new File(Environment.getExternalStorageDirectory(), "POST_IMAGE.jpg");
chooserIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
imageToUploadUri = Uri.fromFile(f);
startActivityForResult(chooserIntent, CAMERA_PHOTO);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_PHOTO && resultCode == Activity.RESULT_OK) {
if(imageToUploadUri != null){
Uri selectedImage = imageToUploadUri;
getContentResolver().notifyChange(selectedImage, null);
Bitmap reducedSizeBitmap = getBitmap(imageToUploadUri.getPath());
if(reducedSizeBitmap != null){
ImgPhoto.setImageBitmap(reducedSizeBitmap);
Button uploadImageButton = (Button) findViewById(R.id.uploadUserImageButton);
uploadImageButton.setVisibility(View.VISIBLE);
}else{
Toast.makeText(this,"Error while capturing Image",Toast.LENGTH_LONG).show();
}
}else{
Toast.makeText(this,"Error while capturing Image",Toast.LENGTH_LONG).show();
}
}
}
这是onActivityResult()中使用的getBitmap()方法.我已经获得了所有可能的性能改进,而这些性能可以在获取相机捕获图像位图的同时实现.
Here is getBitmap() method used in onActivityResult(). I have done all performance improvement that can be possible while getting camera capture image bitmap.
private Bitmap getBitmap(String path) {
Uri uri = Uri.fromFile(new File(path));
InputStream in = null;
try {
final int IMAGE_MAX_SIZE = 1200000; // 1.2MP
in = getContentResolver().openInputStream(uri);
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(in, null, o);
in.close();
int scale = 1;
while ((o.outWidth * o.outHeight) * (1 / Math.pow(scale, 2)) >
IMAGE_MAX_SIZE) {
scale++;
}
Log.d("", "scale = " + scale + ", orig-width: " + o.outWidth + ", orig-height: " + o.outHeight);
Bitmap b = null;
in = getContentResolver().openInputStream(uri);
if (scale > 1) {
scale--;
// scale to max possible inSampleSize that still yields an image
// larger than target
o = new BitmapFactory.Options();
o.inSampleSize = scale;
b = BitmapFactory.decodeStream(in, null, o);
// resize to desired dimensions
int height = b.getHeight();
int width = b.getWidth();
Log.d("", "1th scale operation dimenions - width: " + width + ", height: " + height);
double y = Math.sqrt(IMAGE_MAX_SIZE
/ (((double) width) / height));
double x = (y / height) * width;
Bitmap scaledBitmap = Bitmap.createScaledBitmap(b, (int) x,
(int) y, true);
b.recycle();
b = scaledBitmap;
System.gc();
} else {
b = BitmapFactory.decodeStream(in);
}
in.close();
Log.d("", "bitmap size - width: " + b.getWidth() + ", height: " +
b.getHeight());
return b;
} catch (IOException e) {
Log.e("", e.getMessage(), e);
return null;
}
}
希望对您有帮助!
这篇关于如何在onActivityresult中获取图片的路径(意图数据为空)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!