Android Camera内部保存

时间:2014-01-10 03:28:48

标签: android image save capture

我的问题是以下一个,我需要将我的相机应用程序的图片保存到图库到目前为止无法实现它,任何猜测哪里出错了?只发布最重要的部分......请帮帮我!

  

按下拍照按钮时

public class CameraActivity extends Activity {      
private Camera mCamera;     
private CameraPreview mPreview;    
private String TAG = "CameraActivity";
public static final int MEDIA_TYPE_IMAGE = 1;

@Override     
public void onCreate(Bundle savedInstanceState) {         
    super.onCreate(savedInstanceState);         
    setContentView(R.layout.activity_camera);          

    // Create an instance of Camera  
    mCamera = getCameraInstance();          

    // Create our Preview view and set it as the content of our activity.         
    mPreview = new CameraPreview(this, mCamera);         
    FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);         
    preview.addView(mPreview);     

    Button clickButton = (Button) findViewById(R.id.button_capture);
    clickButton.setOnClickListener(new OnClickListener() {
         @Override
         public void onClick(View v) {
             // TODO Auto-generated method stub

             mCamera.startPreview();
             //mCamera.setPreviewCallback(null);
             mCamera.takePicture(null, null, mPicture);
         }
    });
}

private PictureCallback mPicture = new PictureCallback() {

    @Override
    public void onPictureTaken(byte[] data, Camera camera) {

        File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);

        try {
            FileOutputStream fos = new FileOutputStream(pictureFile);
            fos.write(data);
            fos.close();
        } catch (FileNotFoundException e) {
            //Log.d(TAG, "File not found: " + e.getMessage());
        } catch (IOException e) {
            //Log.d(TAG, "Error accessing file: " + e.getMessage());
        }
    }
};

public static Camera getCameraInstance() {
    Camera c = null;     
    try {         
        c = Camera.open(); // attempt to get a Camera instance     
    }     
    catch (Exception e){         
        // Camera is not available (in use or does not exist)     
    }     return c; // returns null if camera is unavailable }
}


private static File getOutputMediaFile(int type){
    File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile = new File(dir.getPath() + File.separator + "IMG_"+ timeStamp + ".jpg");
    return mediaFile;
}

}

  

找到内部保存的路径

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {      
private SurfaceHolder mHolder;     
private Camera mCamera;     

public CameraPreview(Context context, Camera camera) {         
    super(context);         
    mCamera = camera;          

    // Install a SurfaceHolder.Callback so we get notified when the         
    // underlying surface is created and destroyed.         
    mHolder = getHolder();         
    mHolder.addCallback(this);         
    // deprecated setting, but required on Android versions prior to 3.0         
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);     
}      

public void surfaceCreated(SurfaceHolder holder) {         
    // The Surface has been created, now tell the camera where to draw the preview.         
    try {             
        mCamera.setPreviewDisplay(holder);             
        mCamera.startPreview();         
    } catch (IOException e) {             
        Log.d(VIEW_LOG_TAG, "Error setting camera preview: " + e.getMessage());         
    }     
}      

public void surfaceDestroyed(SurfaceHolder holder) {   
    mCamera.release();
    // empty. Take care of releasing the Camera preview in your activity.     
}      
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {         
    // If your preview can change or rotate, take care of those events here.         
    // Make sure to stop the preview before resizing or reformatting it.          
    if (mHolder.getSurface() == null){           
        // preview surface does not exist           
        return;         
    }          

    // stop preview before making changes         
    try {             
        //mCamera.stopPreview();         
    } catch (Exception e){           
        // ignore: tried to stop a non-existent preview         
    }          
    // set preview size and make any resize, rotate or      
    // reformatting changes here          

    // start preview with new settings         
    try {             
        mCamera.setPreviewDisplay(mHolder);             
        mCamera.startPreview();      
    } catch (Exception e){             
        Log.d(VIEW_LOG_TAG, "Error starting camera preview: " + e.getMessage());         
    }  
}   

}

}

1 个答案:

答案 0 :(得分:1)

mCamera.StartPreview();

之前的

 mCamera.takePicture(null, null, mPicture);

编辑:此外,而不是

 mCamera.takePicture(null, null, mPicture);

使用

 mCamera.takePicture(null, null, null, mPicture);

如果不起作用,请尝试使用

进行调用
 mCamera.takePicture(null, null, null, new PictureCallBack());