Android相机打开然后在新活动中打开捕获的图像

时间:2012-09-18 10:01:10

标签: java android eclipse android-camera

我想创建一个应用程序,当我按下某个按钮时我想打开凸轮然后捕获图像并在我捕获它之后。

我想在新活动中打开此图片,将此图片放入此新活动中,另外还有两个按钮用于删除它,另一个用于将其保存在平板电脑的某个目录中。

我使用代码打开凸轮:

    Open_CAM.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v)  {

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

我不知道在那之后我该怎么办?

任何帮助请...

2 个答案:

答案 0 :(得分:5)

Open_CAM.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v)  {
                Intent photoPickerIntent= new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT, getTempFile());
                photoPickerIntent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
                photoPickerIntent.putExtra("return-data", true);
                startActivityForResult(photoPickerIntent,TAKE_PICTURE);}}

 private Uri getTempFile() 
 {
      File root = new File(Environment.getExternalStorageDirectory(), "ServiceMySigns");
      if (!root.exists()) 
      {
          root.mkdirs();
      }
     final Calendar c = Calendar.getInstance();
     int y = c.get(Calendar.YEAR);
     int m = c.get(Calendar.MONTH);
     int d = c.get(Calendar.DAY_OF_MONTH);

     int h = c.get(Calendar.HOUR_OF_DAY);
     int mi = c.get(Calendar.MINUTE);

      //String filename=""+y+"-"+"-"+(m+1)+"-"+d+" "+h+":"+mi;
      String filename=""+System.currentTimeMillis();
      File file = new File(root,filename+".jpeg" );
      muri = Uri.fromFile(file);
      selectedImagePath=muri.getPath();
      Log.v("take picture path",selectedImagePath);
      return muri;
  }

  public void onActivityResult(int requestcode,int resultcode ,Intent data)
  {
      switch(requestcode)
      { 
      case TAKE_PICTURE: 
          if(resultcode==RESULT_OK)
          { 
              BitmapFactory.Options o = new BitmapFactory.Options();
              o.inSampleSize=8;
              Bitmap newImage = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(selectedImagePath,o), 
                                                         150, 
                                                         150, 
                                                         false);}}}

在onActivityResult中获得位图后,您可以通过意图将该位图发送到另一个活动。

答案 1 :(得分:2)

以下代码将帮助您从相机拍摄照片并将图像设置为下一个活动

private void takePicture() {
     cameraIntent = new Intent(
     android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
     startActivityForResult(cameraIntent, IMAGE_CAPTURE);
}

// Receive the result from the start Activity
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        Log.e("onActivityResult", "we r in onActivityResult");

        if (resultCode == Activity.RESULT_OK) {
            switch (requestCode) {

            case IMAGE_CAPTURE:

                File dir = Environment .getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
                File output = new File(dir, "camerascript.png");
                cPath = output.getAbsolutePath();
                cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                        Uri.fromFile(output));
                Intent capIntent = new Intent(yourcurrentActivity.this,
                        yournextActivity.class);
                capIntent.putExtra("gallery", cPath);
                startActivity(capIntent);
                break;
            default:
                break;
            }
        }
    }

之后获取intnet额外数据到yournextAactivity,你想要设置一个捕获的图像。

        ImageView imageView = (ImageView)findViewById(R.id.imgView);
    String fileString = getIntent().getStringExtra("gallery");
    imageView.setImageBitmap(BitmapFactory.decodeFile(fileString));