开发者

Running my own camera

I'm a beginner to Java and Android, and I have a problem with launching a camera. Precisely I need a small camera preview that would be under my control. (I want to put a sight in the middle of it). I tried to paste this to my project: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/CameraPreview.html But there are loads of errors, after my naive 'fixing', program crashes, before starting anything.. I tried searching google for quite a long time, unsuccessfully. Is somebody in posession of something that would just work without problems? A project would be nice :)

Thanks in adv开发者_如何学运维ance Bye


in your onCreate method, provide the below lines,

String  imgName = getImageName();

    startCamera(imgName);

And below your onCreate, provide these methods. your camera is ready.

   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 = "";
        String strDirectory="/sdcard";
    try {
        imgname = String.format("%d.mp4", 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 Creation", "Directory: " + strDirectoy
                        + " created");
            else
                Log.e("Directory Creation", "Error in Create Directory");
        }

        Log.i("Imagename : ", imgpath);

    } catch (Exception e) {
        Log.e("fileException", e.getMessage());
        e.printStackTrace();
    }

    return imgpath;
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜