我正在开发一个基于相机功能的安卓应用程序。我在我的程序中遇到了一些我无法弄清楚的问题。MymCurrentImagePath
在初始化时始终为空。logcat 显示它永远不会命中takepicture()
函数,因此它永远不会被赋予值。我试图让线程休眠,以便稍后对位图的操作可以等到相机很好地保存图片,但它仍然无法正常工作。但是如果我不看我拍的照片,应用程序运行完美,它可以很好地保存照片。我也尝试使用bitmapfactory.decodebyte()
直接对元数据进行操作,但也失败了。任何人都可以告诉我我错了哪一部分?那真的很有帮助。非常感谢你!!!
private static final String IMAGE_DIRECTORY ="sdcard/DCIM/Camera";
public static String mCurrentImagePath = null;
private CameraView cv;
private Camera mCamera = null;
public Camera.PictureCallback pictureCallback = new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
try{
isFinish = false;
long timeTaken = System.currentTimeMillis();
mCurrentImagePath = IMAGE_DIRECTORY + "/"+ createName(timeTaken) + ".jpg";
File file = new File(mCurrentImagePath);
Log.d("path", "mCurrentImagePath_takepicture = " + mCurrentImagePath);
FileOutputStream fos = new FileOutputStream(file);
fos.write(data);
fos.flush();
fos.close();
Toast.makeText(OpenASURF.this, "saving the picture", Toast.LENGTH_SHORT).show();
isFinish = true;
}
catch(IOException e) {
e.printStackTrace();
}
mCamera.startPreview();
// TODO Auto-generated method stub
}
};
cv.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(mCamera != null) {
mCamera.takePicture(null, null, pictureCallback);
// let the system wait until finish taking the pictures.
//while(isFinish == false) {
try {
Thread.sleep(20000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//}
Log.d("path", "mCurrentImagePath_over1 = " + mCurrentImagePath);
Bitmap bmp = null;
bmp = BitmapFactory.decodeFile(mCurrentImagePath);
Log.d("path", "mCurrentImagePath_over2 = " + mCurrentImagePath);
}