0

我创建了一个基本服务,每 10 秒从相机获取图像。该服务运行良好,但我试图将图像文件命名为ImageYYYYddmm_HHmmSS。在第二个文件之后,图像得到相同的YYYYddmm_HHmmSS,从而覆盖了第一个图像。我的错误在哪里?

public class CP extends Service
{
    Camera.PictureCallback mCall = new Camera.PictureCallback()
  {

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

         FileOutputStream outStream = null;
                try{

                    outStream = new FileOutputStream("/sdcard/Image"+tar+".jpg");
                    outStream.write(data);  outStream.close();


                    Log.i("CAM", data.length + " byte written: /sdcard/Image"+tar+".jpg");
                    camClose(sHolder);               


                 } catch (FileNotFoundException e){
                    Log.d("CAM", e.getMessage());
                } catch (IOException e){
                    Log.d("CAM", e.getMessage());
                }


     }
  };


    @Override
    public IBinder onBind(Intent intent) {

          return null;
    }

    public void camClose(SurfaceHolder sHolder) {


        if (null == mCamera)
            return;
        mCamera.stopPreview();
        mCamera.release();
        mCamera = null;
        Log.i("CAM", " closed");
        }
}
4

1 回答 1

0

Tar 永远不会更新到当前时间。在你的 onPictureTaken 中,尝试这样的事情:

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

     FileOutputStream outStream = null;
            try{

                Calendar cal = Calendar.getInstance();
                tar = (sdf.format(cal.getTime()));
                outStream = new FileOutputStream("/sdcard/Image"+tar+".jpg");
                outStream.write(data);  outStream.close();


                Log.i("CAM", data.length + " byte written: /sdcard/Image"+tar+".jpg");
                camClose(sHolder);               


             } catch (FileNotFoundException e){
                Log.d("CAM", e.getMessage());
            } catch (IOException e){
                Log.d("CAM", e.getMessage());
            }


 }
于 2013-10-19T11:18:59.200 回答