【问题标题】:Open camera application android打开相机应用程序android
【发布时间】:2016-02-18 12:58:42
【问题描述】:

我创建了一个主题为“导航抽屉活动”的项目,我想在左侧菜单中有两个选项:

  • 打开设备相机拍照
  • 打开照片库

这是部分代码:

`

@Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // 处理导航视图项目点击这里。
        int id = item.getItemId();

    if (id == R.id.nav_camera) {
        // Handle the camera action
    } else if (id == R.id.nav_gallery) {

    } else if (id == R.id.nav_slideshow) {

    } else if (id == R.id.nav_manage) {

    } else if (id == R.id.nav_share) {

    } else if (id == R.id.nav_send) {

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

我尝试了几件事,但都没有打开设备相机。

谢谢!

【问题讨论】:

    标签: android android-studio


    【解决方案1】:

    用于启动内置摄像头

    Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);                                                
    startActivityForResult(intent, PIC_CAPTURED);
    

    用于打开图库以选择图片

    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent,
                      "Select Picture"), RESULT_LOAD_IMG);
    

    【讨论】:

    • 谢谢!但是 PIC_CAPTURED 和 RESULT_LOAD_IMG 是什么?
    • 它们是任何常量,只需添加 private int PIC_CAPTURED = 1 和 private int RESULT_LOAD_IMG = 2
    • 如果它有效,别忘了接受答案:)
    • 这些不仅仅是常量。这些是请求代码。不要混淆@ShashankUdupa
    【解决方案2】:

    用于启动相机意图

     Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
     startActivityForResult(intent, 0); // 0 is the request code
    

    用于接收结果图像

      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0) {  //checking if its for our sent request
            if (resultCode == getActivity().RESULT_OK) { //checking if the intent sent result successfully
        Bitmap bp = (Bitmap) data.getExtras().get("data"); //converting camera intent result to bitmap image
       } 
       }
      } 
    

    如何打开图库

     Intent intent = new Intent();
     intent.setType("image/*");
     intent.setAction(Intent.ACTION_GET_CONTENT);//
     startActivityForResult(Intent.createChooser(intent, "Select   Picture"),1111); //1111 is request code
    

    为什么要索取代码:

    使用它是为了当从那个意图接收到结果时,我们可以匹配它是从哪个意图发送的。就像测试的旗帜一样。当从相机接收数据并设置为位图时,您可以在上面的示例代码中看到使用情况。不是你需要加0或1111。你可以放任何整数值

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-08
      相关资源
      最近更新 更多