Android的:以图片并保存在SD卡上的图片 [英] Android: Take picture and save picture on SD Card

查看:202
本文介绍了Android的:以图片并保存在SD卡上的图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要带着我的手机图片通过一个应用程序,并保存我的手机上的图像。
我已经尝试了许多建议对计算器问题的解决方案,但它并不标志着所以我建立了节约与正确名称的文件的方法...但该文件是空的(0KB)!

I want to take a picture with my phone via an application and save the image on my phone. I've tried many of the solutions proposed on the stackoverflow questions but it did not mark so I built a method that saves the file with the right name ... but the file is empty (0kb)!

下面是我的code

public class GameActivity extends Activity implements SurfaceHolder.Callback/*,Camera.PictureCallback*/ {

private Camera camera;
private SurfaceView surfaceCamera;
public Handler handler = new Handler(); 
private boolean isPreview=false;
private SurfaceHolder holder;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gameactivity);
    surfaceCamera = (SurfaceView) findViewById(R.id.surfaceViewCamera);
    getWindow().setFormat(PixelFormat.UNKNOWN);
    holder = surfaceCamera.getHolder();
    holder.addCallback(this);
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    ImageView image = (ImageView) findViewById(R.id.imageView3);
    image.setOnTouchListener(new OnTouchListener(){

        @Override
        public boolean onTouch(View arg0, MotionEvent arg1) {
   camera.takePicture(myShutterCallback, myPictureCallback_RAW, myPictureCallback_JPG);     
            return false;

}


Camera.ShutterCallback myShutterCallback = new Camera.ShutterCallback() {
    public void onShutter() {
      // TODO bl
    }
  };

PictureCallback myPictureCallback_RAW = new PictureCallback() {

    public void onPictureTaken(byte[] arg0, Camera arg1) {
        // TODO Auto-generated method stub

    }
};


PictureCallback myPictureCallback_JPG = new PictureCallback(){

    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
            File imagesFolder = new File(Environment.getExternalStorageDirectory(), "/KersplattFolder");
            imagesFolder.mkdirs(); 
            String fileName = "image.jpg";
            File output = new File(imagesFolder, fileName);
            try {
                FileOutputStream fos = new FileOutputStream(output);
                fos.write(data[0]);
                fos.close();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

                Toast.makeText(GameActivity.this, 
                        "Image saved: ", 
                        Toast.LENGTH_LONG).show();
            camera.startPreview();
            }
};


@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
    if (isPreview) {
        camera.stopPreview();
    }
    Camera.Parameters p = camera.getParameters();
    p.setPreviewSize(width,height);
    camera.setParameters(p);
    try {
        camera.setPreviewDisplay(holder);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    camera.startPreview();
    isPreview=true;
}


@Override
public void surfaceCreated(SurfaceHolder holder) {
    camera = Camera.open();

}


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

}

EDIT1 :当我把数据,而不是数据[0]我救一个黑色图像很好,但文件中有一个权重,所以我想真正的形象是什么地方...

EDIT1 : when I put data instead of data[0] I save a black image as well but the file has a weight so I guess the real image is somewhere...

EDIT2
我已经加入了code

EDIT2 I have added the code

                File output = new File(imagesFolder, fileName);
            Uri outputFileUri = Uri.fromFile(output);
            String filePath = outputFileUri.getPath();
            File file= new File(filePath);
            try {
                FileOutputStream fos = new FileOutputStream(file,true);
                fos.write(data);
                fos.close();
            } 

还有黑色图像

推荐答案

您code:fos.write(数据[0]);是错的。我很惊讶,你没有得到一个异常

Your code: fos.write(data[0]); is wrong. I am surprised that you did not get an exception

下面是code我用的字节数组保存拍照后。 数据是字节数组。

Here is the code I used to save the byte array after taking a picture. "data" is the byte array.

String filePath = outputFileUri.getPath(); // .getEncodedPath();
    File file = new File(filePath);
    FileOutputStream os;
    try {
        os = new FileOutputStream(file, true);
        os.write(data);
        os.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

这篇关于Android的:以图片并保存在SD卡上的图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆