【问题标题】:How can i capture the image and save in the image view in my activity?如何在我的活动中捕获图像并保存在图像视图中?
【发布时间】:2012-07-04 15:54:06
【问题描述】:

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

提前致谢!!!

【问题讨论】:

  • Stackoverflow 不是您的个人编程任务组。请先尝试使用 Google。
  • 把你的代码放在这里..你在你的代码中做了什么?

标签: android android-layout


【解决方案1】:

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

将这两行放在您的按钮单击中..

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";
        }
    }

【讨论】:

    【解决方案2】:

    您可以创建一个方法来捕获图像并在按钮的 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();
                }
            }
        }
    

    【讨论】:

    • 感谢工作!!
    【解决方案3】:

    您的活动代码

    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>
    

    【讨论】:

      【解决方案4】:

      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>
      

      【讨论】:

        【解决方案5】:

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

        `公共类 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>

        【讨论】:

          【解决方案6】:

          你需要在清单中的拳头做到这一点 在 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));
              }
          }
          

          【讨论】:

          • 权限> 这在清单中它会工作 100% 如果工作和你请接受答案如果不告诉我发生了什么坏事
          【解决方案7】:

          试一试:

          第一步:启动相机拍照:

          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);  
                  }  
              }
          

          【讨论】:

          • 查看我的编辑答案并尝试第二种解决方案,确保您在 Manifast.xml 中具有内部存储权限
          猜你喜欢
          • 2023-03-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-06-13
          相关资源
          最近更新 更多