【问题标题】:camera launch on button click in android在android中单击按钮时相机启动
【发布时间】:2011-08-12 21:41:54
【问题描述】:

当我们点击应用中的按钮时,我想打开设备的摄像头。请帮帮我。

【问题讨论】:

  • 如果您想使用在您的应用中拍摄的照片,请使用此处提供的代码tutorial

标签: android


【解决方案1】:

在Button的onClick内,

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

并在清单文件中添加摄像头使用权限。

 <uses-permission android:name="android.permission.CAMERA"></uses-permission>

在此处查看更多讨论Android camera intent

【讨论】:

  • @Kartik 使用您的答案,其他相机应用程序也在选择使用您是否能够纠正只有默认系统相机应用程序才会打开。
  • i got error Can't connect to camera through this 有什么解决办法??
  • 您不需要将android.permission.CAMERA 添加到AndroidManifest.xml,因为您不使用设备摄像头,而只需运行另一个名为Camera 的应用程序,该应用程序使用摄像头.
【解决方案2】:

使用这个

BtnSelectImage.setOnClickListener(new Button.OnClickListener() 
{ 
    @Override
    public void onClick(View v)
    {
        startCamera();
        Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT,
        MediaStore.Images.Media.EXTERNAL_CONTENT_URI.getPath());
        startActivityForResult(intent, 1);
    }
});

【讨论】:

    【解决方案3】:

    首先你需要过滤系统应用程序然后你可以检查相机活动,我只是回答类似的问题here

    【讨论】:

      【解决方案4】:
      #initialize in main activity 
          path = Environment.getExternalStorageDirectory()
                  + "/images/make_machine_example.jpg"; #
          ImageView image=(ImageView)findViewById(R.id.image);
       //--------------------------------------------------||
      
       public void FromCamera(View) {
      
          Log.i("camera", "startCameraActivity()");
          File file = new File(path);
          Uri outputFileUri = Uri.fromFile(file);
          Intent intent = new Intent(
                  android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
          intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
          startActivityForResult(intent, 1);
      
      }
      
      public void FromCard() {
          Intent i = new Intent(Intent.ACTION_PICK,
                  android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
          startActivityForResult(i, 2);
      }
      
       protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      
          super.onActivityResult(requestCode, resultCode, data);
      
          if (requestCode == 2 && resultCode == RESULT_OK
                  && null != 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 picturePath = cursor.getString(columnIndex);
              cursor.close();
      
              bitmap = BitmapFactory.decodeFile(picturePath);
              image.setImageBitmap(bitmap);
      
              if (bitmap != null) {
                  ImageView rotate = (ImageView) findViewById(R.id.rotate);
      
              }
      
          } else {
      
              Log.i("SonaSys", "resultCode: " + resultCode);
              switch (resultCode) {
              case 0:
                  Log.i("SonaSys", "User cancelled");
                  break;
              case -1:
                  onPhotoTaken();
                  break;
      
              }
      
          }
      
      }
      
      protected void onPhotoTaken() {
          // Log message
          Log.i("SonaSys", "onPhotoTaken");
          taken = true;
          imgCapFlag = true;
          BitmapFactory.Options options = new BitmapFactory.Options();
          options.inSampleSize = 4;
          bitmap = BitmapFactory.decodeFile(path, options);
          image.setImageBitmap(bitmap);
      
      
      }
      

      【讨论】:

        猜你喜欢
        • 2013-05-21
        • 2016-07-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-29
        • 1970-01-01
        相关资源
        最近更新 更多