将捕获的图像保存在SD卡上

时间:2012-05-04 07:55:05

标签: android

我正在制作自定义相机应用程序,因此相机在活动视图中打开,但我不知道如何在SD卡上保存捕获的图像?
我在活动视图中使用了流动代码打开相机拍摄的照片:

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

}

请帮忙。

2 个答案:

答案 0 :(得分: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;
        }

答案 1 :(得分: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 );

我认为这段代码会对您有所帮助。也可以验证link