如何在我的活动中捕获图像并保存在图像视图中?

时间:2012-07-04 07:10:48

标签: android android-layout

我有一个活动A,其中我有一个按钮和一个图像视图。当我点击按钮时,它应该加载相机并将图像保存在图像视图中。任何人都可以为此提供代码。

提前致谢!!!

8 个答案:

答案 0 :(得分:3)

见下面的代码..它可能对你有帮助。

将这两行放在按钮上点击..

Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent, 0);

在你的代码中创建这个方法..

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

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

                photo = (Bitmap) data.getExtras().get("data");

                add_image.setImageBitmap(photo); /* this is image view where you want to set image*/

                Log.d("camera ---- > ", "" + data.getExtras().get("data"));


            }
        }
    }

............................................... ............................................

下面的代码为您提供相机拍摄的LastImageID。这只是额外的代码,你想要孔图像,然后它使用 - 你。

............................................... .........................................

private String getLastImageId() {
        final String[] imageColumns = { MediaStore.Images.Media._ID,
                MediaStore.Images.Media.DATA };
        final String imageOrderBy = MediaStore.Images.Media._ID + " DESC";
        Cursor imageCursor = managedQuery(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, imageColumns,
                null, null, imageOrderBy);
        if (imageCursor.moveToFirst()) {
            int id = imageCursor.getInt(imageCursor
                    .getColumnIndex(MediaStore.Images.Media._ID));
            String fullPath = imageCursor.getString(imageCursor
                    .getColumnIndex(MediaStore.Images.Media.DATA));

            imageCursor.close();
            return fullPath;
        } else {
            return "no path";
        }
    }

答案 1 :(得分:0)

这是一个完整的例子,做同样的事情:

Capture Image from Camera and Display in Activity

答案 2 :(得分:0)

尝试:

第1步:启动相机拍摄图像:

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
startActivityForResult(cameraIntent, 2); 

第2步:onActivityResult从数据中获取图片

protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
        if (requestCode == CAMERA_REQUEST) {  
            Bitmap photo = (Bitmap) data.getExtras().get("data"); 
            ImageView imageView=(ImageView) findViewbyId(R.id.imgv);
            imageView.setImageBitmap(photo);
        } 

编辑:和第二个解决方案:

 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  
 intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "temp.jpg")));  
 startActivityForResult(intent, 2);  




 @Override  
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
        if (resultCode == NONE)  
            return;  
        if (requestCode == 2) {   
            File picture = new File(Environment.getExternalStorageDirectory() + "/temp.jpg");  
            ImageView imgView=(ImageView)findViewById(R.id.imgView);
            Uri imgUri=Uri.fromFile(picture);
            imageView.setImageURI(imgUri);  
        }  
    }

答案 3 :(得分:0)

您可以创建一个方法来捕获图像并在按钮的onClickListener中调用它,如下所示:

   public void takePicture(View view){

    try{
    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
    } catch(Exception e){
        e.printStackTrace();
    }
}

然后覆盖onActivityResult方法,如下所示:

     @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    case CAMERA_PIC_REQUEST:

    String path=null;
            try{
                Bitmap thumbnail = (Bitmap) data.getExtras().get("data"); 
                thumbnail = Bitmap.createScaledBitmap(thumbnail,yourDesiredImageWidth, yourDesiredImageHeight, true);
                ImageView imageView = (ImageView) findViewById(R.id.yourImageView);  
                image.setImageBitmap(thumbnail);
                break;
            }catch(Exception ee){
                ee.printStackTrace();
            }
        }
    }

答案 4 :(得分:0)

您的活动代码

public class MainActivity extends Activity {
    /** Called when the activity is first created. */

    Button btnTakePhoto;
    ImageView imgTakenPhoto;
    private static final int CAM_REQUREST = 1313;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        btnTakePhoto = (Button) findViewById(R.id.button1);
        imgTakenPhoto = (ImageView) findViewById(R.id.imageView1);

        btnTakePhoto.setOnClickListener(new btnTakePhotoClicker());
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);

          if (requestCode == CAMERA_PIC_REQUEST && resultCode == RESULT_OK) {

                  if (data != null) {
              Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
              imgTakenPhoto.setImageBitmap(thumbnail);
                  }
          }
    }

    class btnTakePhotoClicker implements Button.OnClickListener
    {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
        }
    }
}

您的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Press the button, capture photo and enjoy!!"
        android:id="@+id/textview1"/>

    <Button
        android:text="Take Photo!"
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textview1">
    </Button>

    <ImageView
        android:id="@+id/imageView1"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:src="@drawable/icon"
        android:scaleType="centerCrop"
        android:layout_below="@+id/button1">
    </ImageView>
</RelativeLayout>

答案 5 :(得分:0)

CemraImageActivity.java

public class CemraImageActivity extends Activity implements OnClickListener{
    private int CAMERA_REQUEST;
    private ImageView imageView;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);  

        this.imageView = (ImageView)this.findViewById(R.id.image_capture);
        imageView.setOnClickListener(this);           

    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if(resultCode != 0)
        {
            if (requestCode == CAMERA_REQUEST) 
            {
                Bitmap photo = (Bitmap) data.getExtras().get("data");
                imageView.setImageBitmap(photo);        
            }
        }
    }

    public void onClick(View v) {
        // TODO Auto-generated method stub
        Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);  
        startActivityForResult(cameraIntent, CAMERA_REQUEST); 
    }  
}

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="334dp"
        android:layout_height="wrap_content"
        android:text="Click this image for take a photo"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <ImageView
        android:id="@+id/image_capture"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.06"
        android:src="@drawable/ic_launcher" />

</LinearLayout>

答案 6 :(得分:0)

获取Click按钮并在imageview中设置商店图片的非常简单的方法。

  

`public class MainActivity扩展了Activity {

private static final int CAMERA_PIC_REQUEST = 22;

Uri cameraUri;

Button BtnSelectImage;
private ImageView ImgPhoto;
private String Camerapath ;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ImgPhoto = (ImageView) findViewById(R.id.imageView1);

    BtnSelectImage = (Button) findViewById(R.id.button1);
    BtnSelectImage.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            // TODO Auto-generated method stub
            try {
                   Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                   startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), "Couldn't load photo", Toast.LENGTH_LONG).show();
            }
        }
    });

}


@Override
public void onActivityResult(final int requestCode, int resultCode, Intent data) {
    try {
        switch (requestCode) {
       case CAMERA_PIC_REQUEST:
            if (resultCode == RESULT_OK) {
                try {
                      Bitmap photo = (Bitmap) data.getExtras().get("data"); 

                      ImgPhoto.setImageBitmap(photo);    

                } catch (Exception e) {
                    Toast.makeText(this, "Couldn't load photo", Toast.LENGTH_LONG).show();
                }
            }
            break;
          default:
            break;
        }
    } catch (Exception e) {
    }
}

}`

设置此清单。

  

<uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" android:required="false"></uses-feature>

答案 7 :(得分:0)

  你需要在清单中做到这一点              在mainactivity.java之后执行此操作

public class MainActivity extends Activity {
    Button button;
    ImageView photo;
    static final int Cam_Request = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button)findViewById(R.id.btnPhoto);
        photo = (ImageView)findViewById(R.id.ivPhoto);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent camera_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                File file = getFile();
                camera_intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
                startActivityForResult(camera_intent, Cam_Request);


            }
        });
    }
    private File getFile ()
    {
        File folder = new File("sdcard/camera_app");

        if(!folder.exists())
        {
            folder.mkdir();
        }

        File image = new File(folder,"image.jpg");
        return image;
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        String path = "sdcard/camera_app/image.jpg";
        photo.setImageDrawable(Drawable.createFromPath(path));
    }
}