[英]capturing image using camera in Android
Hi stackoverflow friends, I need to take a picture using camera and after takin the picture go to next activity without showing the first activity.嗨 stackoverflow 朋友,我需要使用相机拍照,然后将图片 go 拍摄到下一个活动而不显示第一个活动。 And display it in an imageview.
并将其显示在 imageview 中。 Below is the flow of my application
以下是我的申请流程
first activity-> there is button for camera intent->go to the next activity(without showing the fist activity) second activity->there i need to show the image in imageview.第一个活动-> 有相机意图按钮-> 转到下一个活动(不显示第一个活动)第二个活动-> 我需要在 imageview 中显示图像。
I saw a lot of examples of camera intent nobody explains how to go to the next activity without showing the first and display it in imageview of second.我看到了很多相机意图的例子,没有人解释如何在不显示第一个活动的情况下将 go 转到下一个活动,并将其显示在第二个活动的 imageview 中。
Any outofmemeory problem occurs while displaying images in imageview repeatedly?重复显示 imageview 中的图像时是否出现内存不足问题?
Thanks in advance提前致谢
In First activity:在第一个活动中:
Button b=(Button)findViewByid(R.id.button);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
doTakePhotoAction();
}
});
private void doTakePhotoAction() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
mUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),
"pic_" + String.valueOf(System.currentTimeMillis()) + ".jpg"));
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mUri);
try {
intent.putExtra("return-data", true);
startActivityForResult(intent, CAMERA_RESULT);
// finish();
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
}
protected void onActivityResult(int requestCode,
int resultCode, Intent data) {
if (resultCode != RESULT_OK) {
return;
}
if (requestCode == CAMERA_RESULT) {
Intent intent = new Intent(this, nextimage.class);
// here you have to pass absolute path to your file
intent.putExtra("image-path", mUri.getPath());
intent.putExtra("scale", true);
startActivity(intent);
finish();
}
}
In nextimage.class you can set one image view and get the imagepath from putExtra and place it in imageview.在 nextimage.class 中,您可以设置一个图像视图并从 putExtra 获取图像路径并将其放置在 imageview 中。
String mImagePath = extras.getString("image-path");
Bitmap mBitmap = getBitmap(mImagePath);
private Uri getImageUri(String path) {
return Uri.fromFile(new File(path));
}
private Bitmap getBitmap(String path) {
Uri uri = getImageUri(path);
InputStream in = null;
try {
in = mContentResolver.openInputStream(uri);
return BitmapFactory.decodeStream(in);
} catch (FileNotFoundException e) {
Log.e(TAG, "file " + path + " not found");
}
return null;
}
place the bitmap in the imageview.you have to create imageview in secondActivity.将 bitmap 放在 imageview 中。您必须在 secondActivity 中创建 imageview。
use a camera intent....here's a simple code使用相机意图....这是一个简单的代码
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.widget.ImageView;
public class CameraIntent extends Activity {
final static int CAMERA_RESULT = 0;
ImageView imv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, CAMERA_RESULT);
}
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (resultCode == RESULT_OK)
{
Get Bundle extras = intent.getExtras();
Bitmap bmp = (Bitmap) extras.get("data");
imv = (ImageView) findViewById(R.id.ReturnedImageView);
imv.setImageBitmap(bmp);
}
}
} }
Use Following Code for that, it will solve your problem.为此使用以下代码,它将解决您的问题。
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
onActivityResult() Method:- onActivityResult() 方法:-
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == CAMERA_PIC_REQUEST) {
bmpImage = (Bitmap) data.getExtras().get("data");
drawable = new BitmapDrawable(bmpImage);
mImageview.setImageDrawable(drawable);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.