0

我是 Android 新手,我写了一个小程序来拍照并将它们保存在内部存储磁盘中。我的程序成功启动,但是当我拍照时,没有数据写入内存磁盘。

下面是我的代码。我错过了什么?

package com.lab2;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.os.Environment;

public class Lab2_cameraActivity extends Activity implements SurfaceHolder.Callback {
/** Called when the activity is first created. */
private Camera mCamera;
private SurfaceHolder mHolder;
private SurfaceView mSurfaceView;
private Button mbutton1;
private ShutterCallback shutter;
private PictureCallback raw;
private PictureCallback postview;



AutoFocusCallback AFCallback = new AutoFocusCallback(){ 
    @Override 
    public void onAutoFocus(boolean success, Camera camera) { 
        // TODO Auto-generated method stub 
        if (success){               
            mCamera.takePicture(shutter, raw, postview, jpeg);//take photo 
        }else{ 
            Toast.makeText(Lab2_cameraActivity.this, "in else...",Toast.LENGTH_LONG).show();
        } 
    } 
   };

   /*jpeg callback occurs when the compressed image is available*/ 
        PictureCallback jpeg = new PictureCallback(){ 
            @Override 
            public void onPictureTaken(byte[] data, Camera camera) { 
                // TODO Auto-generated method stub 
                FileOutputStream outStream = null;                                        
                File file = new File("/mnt/sdcard/");              
                try {                       
                    outStream = new FileOutputStream(file.toString()+"photo.jpg");                                              
                    outStream.write(data);              
                    outStream.close(); 
                    Toast.makeText(Lab2_cameraActivity.this, "Photo Saved...",Toast.LENGTH_LONG).show();                 
                    mCamera.startPreview();              
                } catch (FileNotFoundException e) { 
                    // TODO Auto-generated catch block 
                    e.printStackTrace(); 
                } catch (IOException e) { 
                    // TODO Auto-generated catch block 
                    e.printStackTrace(); 
                } 
            } 
           };

   public void surfaceDestroyed(SurfaceHolder holder) { 
        // TODO Auto-generated method stub 
        /*when surface destroyed, stop preview*/ 
        if(mCamera != null){ 
            mCamera.stopPreview(); 
            mCamera.release();//release camera resources 
            mCamera = null; 
        } 

       } 



public void surfaceCreated(SurfaceHolder holder) { 
    // TODO Auto-generated method stub 
    mCamera = Camera.open();//open camera 
    try { 
        mCamera.setPreviewDisplay(holder);//set surface to display 
    } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
    } 
   } 

public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { 
    // TODO Auto-generated method stub
    /*when surface changed, reset camera parameters for preview*/ 
    mCamera.getParameters();//get default parameters
    Camera.Parameters parameters= mCamera.getParameters(); 
    /*you can set your own parameters in there*/ 
    mCamera.setParameters(parameters);//set parameters if parameters has been changed 
    mCamera.startPreview(); 
   }

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);       
    mbutton1 = (Button)findViewById(R.id.button1);
    mSurfaceView = (SurfaceView)findViewById(R.id.surfaceView);
    mHolder = mSurfaceView.getHolder();
    mHolder.addCallback(this);
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    mbutton1.setOnClickListener(new Button.OnClickListener() 
    { 
        @Override 
        public void onClick(View v) { 
            mCamera.autoFocus(AFCallback); 
        } 
    }); 



}

}

4

1 回答 1

1
  1. 您不应指定 '/mnt/whatever/' 到您的 sdcard 的路径,getExternalFilesDir()用于获取正确的路径。

  2. 你的回调不会被调用,除非你在mCamera.takePicture(null, null, jpeg);

  3. 你真的有吗

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">
    

在您的 AndroidManifest.xml 文件中?

于 2012-05-01T10:49:14.623 回答