2013-11-22 90 views
-1

我不知道我做錯了,分離似乎是好的,但圖片保持覆蓋,我怎樣才能更改代碼,以便圖片每次獲得tdiferents名稱,即使我不關閉應用程序?圖片保留覆蓋

public class MirrorActivity extends Activity implements PictureCallback { 
private final static String DEBUG_TAG = "MirrorActivity"; 
private Camera mCam; 
private MirrorView mCamPreview; 
private int mCameraId = 0; 
private FrameLayout mPreviewLayout; 


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



    // do we have a camera? 
    if (!getPackageManager() 
      .hasSystemFeature(PackageManager.FEATURE_CAMERA)) { 
     Toast.makeText(this, "No camera feature on this device", 
       Toast.LENGTH_LONG).show(); 
    } else { 

     mCameraId = findFirstFrontFacingCamera(); 

     if (mCameraId >= 0) { 
      mPreviewLayout = (FrameLayout) findViewById(R.id.camPreview); 
      mPreviewLayout.removeAllViews(); 

      startCameraInLayout(mPreviewLayout, mCameraId); 

      Button takePic = (Button) findViewById(R.id.capture); 
      takePic.setOnClickListener(new View.OnClickListener() { 

       public void onClick(View v) { 
        mCam.takePicture(null, null, MirrorActivity.this); 

       } 
      }); 
     } else { 
      Toast.makeText(this, "No front facing camera found.", 
        Toast.LENGTH_LONG).show(); 
     } 
    } 
} 


@SuppressLint("NewApi") 
private int findFirstFrontFacingCamera() { 
    int foundId = -1; 
    // find the first front facing camera 
    int numCams = Camera.getNumberOfCameras(); 
    for (int camId = 0; camId < numCams; camId++) { 
     CameraInfo info = new CameraInfo(); 
     Camera.getCameraInfo(camId, info); 
     if (info.facing == CameraInfo.CAMERA_FACING_BACK) { 
      Log.d(DEBUG_TAG, "Found back facing camera"); 
      foundId = camId; 
      break; 
     } 
    } 
    return foundId; 
} 

private void startCameraInLayout(FrameLayout layout, int cameraId) { 

    // TODO pull this out of the UI thread. 
    mCam = Camera.open(cameraId); 
    if (mCam != null) { 
     mCamPreview = new MirrorView(this, mCam); 
     layout.addView(mCamPreview); 
    } 
} 

@Override 
protected void onResume() { 
    super.onResume(); 
    if (mCam == null && mPreviewLayout != null) { 
     mPreviewLayout.removeAllViews(); 
     startCameraInLayout(mPreviewLayout, mCameraId); 
    } 
} 

@Override 
protected void onPause() { 
    if (mCam != null) { 
     mCam.release(); 
     mCam = null; 
    } 
    super.onPause(); 
} 

public void onPictureTaken(byte[] data, Camera camera) { 
    File pictureFileDir = new File(
      Environment 
        .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), 
      "PhotoGalleryNobattery"); 

    if (!pictureFileDir.exists() && !pictureFileDir.mkdirs()) { 

     Log.d(DEBUG_TAG, "Can't create directory to save image"); 
     Toast.makeText(this, "Can't make path to save pic.", 
       Toast.LENGTH_LONG).show(); 



     return; 



    } 

    String filename = pictureFileDir.getPath() + File.separator 
      + "Picture.jpg"; 

    File pictureFile = new File(filename); 



    try { 
     FileOutputStream fos = new FileOutputStream(pictureFile); 
     fos.write(data); 
     fos.close(); 
     Toast.makeText(this, "Image saved as Picture.jpg", 
       Toast.LENGTH_LONG).show(); 
    } catch (Exception error) { 
     Log.d(DEBUG_TAG, "File not saved: " + error.getMessage()); 
     Toast.makeText(this, "Can't save image.", Toast.LENGTH_LONG).show(); 

     Intent intent =new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); 
     intent.setData(Uri.fromFile(pictureFile)); 
     sendBroadcast(intent);  
    } 
} 

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

    public MirrorView(Context context, Camera camera) { 
     super(context); 
     mCamera = camera; 
     mHolder = getHolder(); 
     mHolder.addCallback(this); 
     mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
    } 

    public void surfaceCreated(SurfaceHolder holder) { 
     try { 
      mCamera.setPreviewDisplay(holder); 
      mCamera.startPreview(); 
     } catch (Exception error) { 
      Log.d(DEBUG_TAG, 
        "Error starting mPreviewLayout: " + error.getMessage()); 
     } 
    } 

    public void surfaceDestroyed(SurfaceHolder holder) { 
    } 

    public void surfaceChanged(SurfaceHolder holder, int format, int w, 
      int h) { 
     if (mHolder.getSurface() == null) { 
      return; 
     } 

     // can't make changes while mPreviewLayout is active 
     try { 
      mCamera.stopPreview(); 
     } catch (Exception e) { 

     } 

     try { 
      // set rotation to match device orientation 
      setCameraDisplayOrientationAndSize(); 

      // start up the mPreviewLayout 
      mCamera.setPreviewDisplay(mHolder); 
      mCamera.startPreview(); 

     } catch (Exception error) { 
      Log.d(DEBUG_TAG, 
        "Error starting mPreviewLayout: " + error.getMessage()); 
     } 

    } 

    public void setCameraDisplayOrientationAndSize() { 
     CameraInfo info = new CameraInfo(); 
     Camera.getCameraInfo(mCameraId, info); 
     int rotation = getWindowManager().getDefaultDisplay().getRotation(); 
     int degrees = rotation * 90; 

     /* 
     * // the above is just a shorter way of doing this, but could break 
     * if the values change switch (rotation) { case Surface.ROTATION_0: 
     * degrees = 0; break; case Surface.ROTATION_90: degrees = 90; 
     * break; case Surface.ROTATION_180: degrees = 180; break; case 
     * Surface.ROTATION_270: degrees = 270; break; } 
     */ 

     int result; 
     if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { 
      result = (info.orientation + degrees) % 360; 
      result = (360 - result) % 360; 
     } else { 
      result = (info.orientation - degrees + 360) % 360; 
     } 
     mCamera.setDisplayOrientation(result); 

     Camera.Size previewSize = mCam.getParameters().getPreviewSize(); 
     if (result == 90 || result == 270) { 
      // swap - the physical camera itself doesn't rotate in relation 
      // to the screen ;) 
      mHolder.setFixedSize(previewSize.height, previewSize.width); 
     } else { 
      mHolder.setFixedSize(previewSize.width, previewSize.height); 

     } 
    } 

} 

回答

0

我相信它,因爲你總是使用相同的文件名。請參見下面的內部onPictureTaken代碼的一部分:

String filename = pictureFileDir.getPath() + File.separator 
     + "Picture.jpg"; 

onPictureTaken傳遞每次調用該函數的時間有不同的價值的附加參數filename。它可以是currentDate_CuentTimeInMilliSeconds.jpg

+0

我該怎麼做? – user3001794

+0

我給你的想法。把它分解成幾部分,你可以得到當前的時間和日期。然後將其轉換爲字符串並將其作爲參數發送。搜索所有3個部分將回答你如何做每個部分。希望這可以幫助。 –