如何使用android设备原生相机..?

时间:2011-12-20 13:16:43

标签: android

我正在开发一个应用程序,我想使用设备本机camera.i.e。我想使用设备将从设备点击相机选项时显示的精确相机,包括前置摄像头选项等小部件。我在Android市场上看到了一个应用程序正是这样做的。谁能帮助我?

谢谢, 拉姆。

1 个答案:

答案 0 :(得分:0)

如果您想将图像保存在文件夹中,那么这是最简单的方法:

 try {
    //where to save the image

                File root = new File(Environment.getExternalStorageDirectory()
                        + File.separator + "foldername" + File.separator);
                root.mkdirs();
                sdImageMainDirectory = new File(root, "myPicName.jpg");
                 outputFileUri = Uri.fromFile(sdImageMainDirectory);
                startCameraActivity();
            } catch (Exception e) {

                Toast.makeText(this, "Error occured. Please try again later.",
                        Toast.LENGTH_SHORT).show();
                finish();
            }



    protected void startCameraActivity() {



            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
            startActivityForResult(cameraIntent, 101);


        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);

            if(requestCode==101 && resultCode==-1)
            {
                try
                {

            //use that file here


                }
                catch(Exception ex)
                {
                    Log.v("OnCameraCallBack",ex.getMessage());


                }
            }

如果您想将其保存在默认位置:

 Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
           startActivityForResult(cameraIntent, 101);
 @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);

            if(requestCode==101 && resultCode==-1)
            {
                try
                {

            //use that file here


                }
                catch(Exception ex)
                {
                    Log.v("OnCameraCallBack",ex.getMessage());


                }
            }