简体   繁体   English

使用 Android 中的相机拍摄图像

[英]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.

相关问题 Android 使用相机意图捕获图像后应用程序崩溃 - Android App crashes after capturing image using camera intent 从 Android 中的相机捕获图像然后裁剪 - Capturing image from Camera in Android and then cropping 使用相机拍摄时将图像旋转到移动设备的方向:Android - Rotating image to Mobile's Orientation when capturing with camera :Android 从相机捕获图像并将其显示在android中的另一个活动中 - Capturing Image from camera and displaying it in another activity in android 在Android中从相机捕获后,有什么方法可以检测图像是否模糊(OpenCV除外) - Is there any way to detect image is Blurry after capturing from Camera in Android (Except OpenCV ) 从相机捕获图像时,位图为空 - Bitmap is null while capturing image from camera 缩放后在Camera2 Api中捕获图像 - Image Capturing in Camera2 Api after zooming 捕获图像camera2时预览锁定 - Preview locked while Capturing Image camera2 从相机捕获图像并将其设置为android中的listview - capturing images from camera and setting into listview in android 尝试通过使用默认相机应用程序使用Firebase并捕获图像并上传到Firebase存储 - Trying to work with Firebase by using Default camera app and capturing the Image and uploading to the Firebase Storage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM