打开相机应用android

时间:2016-02-18 12:58:43

标签: android android-studio

我创建了一个主题为"导航抽屉活动"我想在左侧菜单上有两个选项:

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

以下是代码的一部分:

`

@Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        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; }

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

谢谢!

2 个答案:

答案 0 :(得分:2)

启动内置摄像头

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

答案 1 :(得分:0)

用于启动相机意图

 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

为什么要求代码:

它被使用,以便当从该意图接收到结果时,我们可以匹配从哪个意图发送它。就像一个测试标志。从相机接收数据并设置为Bitmap时,您可以在上面的示例代码中看到用法。它不是你需要添加0或1111.你可以放任何整数值