从相机捕获图像并将图像加载到另一个活动的imageview中

时间:2014-01-15 18:01:53

标签: android xml

我在将相机拍摄的图像提取到另一个活动的图像视图中时遇到问题所以请找出问题.....

      public void onClick(View v) {

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

                    startActivityForResult(cameraIntent, CAMERA_REQUEST);   

和onActivityresult()是

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

       super.onActivityResult(requestCode, resultCode, data);
       if(requestCode==CAMERA_REQUEST&&resultCode==RESULT_OK)
         {
           String[] projection = { MediaStore.Images.Media.DATA}; 

           Cursor cursor = managedQuery(mCapturedImageURI, projection, null, null, null); 
           int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
           cursor.moveToFirst(); 
           String picturepath = cursor.getString(column_index_data);
           Log.d("TAG", "getLastImageId::path " + picturepath);
           Intent   camintent= new Intent(MainActivity.this,GalleryActivity.class);
             camintent.putExtra("imagePath",picturepath );
             startActivity(camintent);
 }
}

我的另一个(接收)活动是........

      private void getData(){

        String ps=getIntent().getStringExtra("imagePath");
        img.setImageBitmap(BitmapFactory.decodeFile(ps));
     }

2 个答案:

答案 0 :(得分:1)

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

       super.onActivityResult(requestCode, resultCode, data);
       if(requestCode==CAMERA_REQUEST&&resultCode==RESULT_OK)
         {
           Bitmap bitmapImage = (Bitmap) intent.getExtras().get("data");
           Intent   camintent= new Intent(MainActivity.this,GalleryActivity.class);
           camintent.putExtra("bitmap", bitmapImage);
           startActivity(camintent);
 }
}

并在第二个Activity中检索位图图像

private void getData(){

        Bitmap bitImage=getIntent().getParcelableExtra("bitmap");
        img.setImageBitmap(bitImage);

     }

答案 1 :(得分:0)

在此处查看有关如何启动相机然后检索图像的示例: Capture Image from Camera and Display in Activity

protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {  
        Bitmap photo = (Bitmap) data.getExtras().get("data"); 
        imageView.setImageBitmap(photo);
    }  
} 

如果要在从相机接收图像后在两个不同的活动之间传递结果,可以将位图保存为文件,然后在第二个活动中读取它。