开发者

How to take photo using Camera API?

I want to know how to capture photo using Ca开发者_如何学Pythonmera API and save in camera folder or in gallery. Please help me with some sample code.

Thanks Monali


Try this out.. Add this in your onCreate(), String img = getImageName(); Now Call the below method in your onCreate() and should do the trick.

private void startCamera(String ImageName) {



    Intent cameraIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);


    cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
            Uri.fromFile(new File(ImageName)));

    startActivityForResult(cameraIntent, TAKE_PICTURE_WITH_CAMERA);
}

private String getImageName() {
    String imgname = "";
    String imgpath = "";
    try {
        imgname = String.format("%d.png", System.currentTimeMillis());

        imgpath = strDirectoy + "/" + imgname;

        File file = new File(strDirectoy);
        boolean exists = file.exists();
        if (!exists) {
            boolean success = (new File(strDirectoy)).mkdir();
            if (success)
                Log.e("Directory Created", "Directory: " + strDirectoy
                        + " created");
            else

                Log.e("Directory Creation","Directory Creation failed");
        }



    } catch (Exception e) {

        e.printStackTrace();
    }

    return imgpath;
}


use following code in mainActivity:

    public class MainActivity extends Activity implements OnClickListener {

        private static final int CAMERA_REQUEST= 1888;

        private Button photoBtn;
        private ImageView imageView;

        @Override
        public void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
               setContentView(R.layout.activity_main);

               photoBtn=(Button) findViewById(R.id.photobtn);
               photoBtn.setOnClickListener(this);

               imageView=(ImageView) findViewById(R.id.imageview);
        }

        @Override
        public void onClick(View v) {

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

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

               if(requestCode == CAMERA_REQUEST && resultCode == RESULT_OK){

                    Bitmap photo=(Bitmap) data.getExtras().get("data");
                    imageView.setImageBitmap(photo);

                }

         }
   }

-- Add following line in menifest.xml file

            <uses-feature android:name="android.hardware.camera" />

-- use activity_main.xml layout is:

         <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                <Button
                       android:id="@+id/photobtn"
                       style="?android:attr/buttonStyleSmall"
                       android:layout_width="wrap_content"
                       android:layout_height="wrap_content"
                       android:layout_alignParentTop="true"
                       android:layout_centerHorizontal="true"
                       android:text="Open Camera" />

                 <ImageView
                           android:id="@+id/imageview"
                           android:layout_width="wrap_content"
                           android:layout_height="wrap_content"
                           android:layout_below="@+id/photobtn"
                           android:layout_centerHorizontal="true"
                           android:src="@drawable/ic_launcher" />

         </RelativeLayout>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜