从相机捕获图像并将其显示在android中的另一个活动中 [英] Capturing Image from camera and displaying it in another activity in android

查看:130
本文介绍了从相机捕获图像并将其显示在android中的另一个活动中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,在该应用程序中我想从相机捕获图像并将其显示在图像视图的另一个活动中,我的问题是能够捕获图像,但是在捕获后我将重定向到第一个活动,而不是第二个.

I am developing an application in which i want to capture the image from the camera and display it in another activity in an image view, my problem is that able to capture the image but after capturing i am redirected to first activity instead to second one.

这是我的代码. PictureOptions.java

public void buttonCameraOpen(View view)
{
    // create Intent to take a picture and return control to the calling application
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name

    // start the image capture Intent
    startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}

private static Uri getOutputMediaFileUri(int type){
      return Uri.fromFile(getOutputMediaFile(type));
}

/** Create a File for saving an image or video */
private static File getOutputMediaFile(int type){
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.

    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
              Environment.DIRECTORY_PICTURES), "Easy Heal");
    // This location works best if you want the created images to be shared
    // between applications and persist after your app has been uninstalled.

    // Create the storage directory if it does not exist
    if (! mediaStorageDir.exists()){
        if (! mediaStorageDir.mkdirs()){
            Log.d("MyCameraApp", "failed to create directory");
            return null;
        }
    }

 // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile;
    if (type == 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) {
     Bitmap selectedphoto   = null;

     super.onActivityResult(requestCode, resultCode, data);

     if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE  && resultCode == RESULT_OK && null!=data) {

            // Image captured and saved to fileUri specified in the Intent
            //selectedphoto = (Bitmap) data.getExtras().get("data");
            Uri selectedImage = data.getData();
             String [] filePathColumn = {MediaStore.Images.Media.DATA};
             Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
             cursor.moveToFirst();   
             int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
             String filePath = cursor.getString(columnIndex);
             File f =new File(filePath);
             String filename = f.getName();
             cursor.close();
             selectedphoto = BitmapFactory.decodeFile(filePath);

             Intent intent = new Intent(PictureOptions.this,ShowImage.class);
             //intent.putExtra("data", selectedphoto);
             intent.setData( selectedImage );
             startActivity(intent);
        } else if (resultCode == RESULT_CANCELED) {
            // User cancelled the image capture
        } else {
            // Image capture failed, advise user
       }

}

PictureOptions.xml

<Button
    android:id="@+id/buttonCameraOpen"
    android:layout_width="fill_parent"
    android:layout_height="72dp"
    android:layout_weight="0.35"
    android:onClick="buttonCameraOpen"
    android:text="@string/button_camera_open" />

ShowImage.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_show_image);
    ImageView imageview = (ImageView)findViewById(R.id.ImageShow);
    Uri imageUri = getIntent().getData();
    //Bitmap selectedphoto  =(Bitmap)this.getIntent().getParcelableExtra("data");
    imageview.setImageURI(imageUri);
}

ShowImage.xml

 <ImageView
    android:id="@+id/ImageShow"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

推荐答案

我终于找到了一个很棒的解决方案 该库用于从相机捕获图像或从图库中选择图像,并以onActivityResult方法返回File格式的图像,该图像可在应用程序中进一步使用. 使用

I finally found an awesome solution This library is used to capture image from camera or select from gallery and return back image in File format in onActivityResult method, which can be used further in the application. Use

EasyImage库

这篇关于从相机捕获图像并将其显示在android中的另一个活动中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆