0

我正在开发一个基于相机功能的安卓应用程序。我在我的程序中遇到了一些我无法弄清楚的问题。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);
        }
4

2 回答 2

1

是否IMAGE_DIRECTORY正确?我希望它是/sdcard/DCIM/Camera,带有开头的正斜杠。

更好的是以正确的方式获取图像路径:DIRECTORY_PICTURES

File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File file = new File(path, "DemoPicture.jpg");
于 2012-09-18T20:51:27.147 回答
1

不管你等多久takePicture()onPictureTaken()回调只会在你OnClick()返回后被调用:它们都使用应用程序主线程。

你可以打电话

BitmapFactory.decodeFile(mCurrentImagePath);

在您的 中onPictureTaken(),或者您可以将事件发布onPictureTaken()到您的活动中。

于 2012-09-19T07:07:42.757 回答