【问题标题】:android camera save file errorandroid相机保存文件错误
【发布时间】:2012-09-19 15:07:42
【问题描述】:

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

【问题讨论】:

    标签: android camera


    【解决方案1】:

    无论你在takePicture()之后等待多久,onPictureTaken()回调只会在你的OnClick()返回后被调用:它们都使用应用主线程。

    你可以打电话

    BitmapFactory.decodeFile(mCurrentImagePath);
    

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

    【讨论】:

    • 感谢您的建议,这让我对这个问题有了清晰的认识。我尝试将计算部分添加到 onPictureTaken() 中,但它也显示 OutofMemoryError。你有什么想法吗?
    • 我将使用从相机拍摄的照片进行图像识别。我将计算其 SURF 描述符以进行进一步匹配。
    • 我的意思是 - 你做了什么来摆脱内存?你在日志中看到了什么?但这可能值得自己提出一个问题。
    • 我已经完成了应用程序,感谢您的帮助,我使用 AsynTask 进行后端计算,效果很好。
    【解决方案2】:

    IMAGE_DIRECTORY 正确吗?我希望它是/sdcard/DCIM/Camera,带有正斜杠。

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

    File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    File file = new File(path, "DemoPicture.jpg");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-20
      • 1970-01-01
      • 2021-01-10
      • 1970-01-01
      • 1970-01-01
      • 2014-10-02
      • 1970-01-01
      • 2015-11-05
      相关资源
      最近更新 更多