-1

I am making an custom camera application, so camera is open in activity view, but I don't know how to save captured image on sdcard?
I have used flowing code to open camera in activity view captured photo:

public class Camera_preview extends Activity implements SurfaceHolder.Callback,Camera.PictureCallback, OnClickListener {

    Camera camera;
    SurfaceView cameraView;
    SurfaceHolder surfaceHolder;
    Button Photo_btn;
     public void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.camera_layout);
            Photo_btn =(Button)findViewById(R.id.camera_btn);
            cameraView = (SurfaceView) this.findViewById(R.id.CameraView);
            surfaceHolder = cameraView.getHolder();
            surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
            surfaceHolder.addCallback(this);
            actionBar.hide();
            Photo_btn.setOnClickListener(this);
        }
    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        // TODO Auto-generated method stub

    }  

    @Override
    public void surfaceCreated(SurfaceHolder holder) 
    {
        // TODO Auto-generated method stub  
        camera =Camera.open();  
        try
        {
        camera.setPreviewDisplay(holder);
        }
        catch (IOException exception)
        {
        camera.release();
        }
        camera.startPreview();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        camera.stopPreview();
        camera.release();

    }
    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
        // TODO Auto-generated method stub

        System.out.println("******data*********"+data);
        camera.startPreview();
    }
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if(v == Photo_btn)
        {
            camera.takePicture(null, null, this);
        }
    }

}

Please help.

4

2 に答える 2

3
Try this code...

private static final int CAMERA_REQUEST = 1888;  
    buttonTakePicture.setOnClickListener(new Button.OnClickListener() {
                @Override
                public void onClick(View arg0) {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    intent.putExtra(MediaStore.EXTRA_OUTPUT, getImageUri());

    startActivityForResult(intent, CAMERA_REQUEST);
                    }
            });

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

            if (requestCode == CAMERA_REQUEST) {
                    photo = (Bitmap) data.getExtras().get("data");
                }
    }

    private Uri getImageUri() {


            File file1 = new File(Environment.getExternalStorageDirectory() + "/Camerafolder");
            if (!file1.exists())
            {
                file1.mkdirs();
            }

            File file = new File(Environment.getExternalStorageDirectory() + "/Camerafolder/"+"img"+".png");

    Uri imgUri = Uri.fromFile(file);

            return imgUri;
        }
于 2012-05-04T09:54:53.640 に答える
0
    _path = Environment.getExternalStorageDirectory() + "make_machine_example.jpg";
File file = new File( _path );
Uri outputFileUri = Uri.fromFile( file );

Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
intent.putExtra( MediaStore.EXTRA_OUTPUT, outputFileUri );

startActivityForResult( intent, 0 );

このコードが役立つと思います。このリンクも確認してください

于 2012-05-04T08:36:12.967 に答える