如何更改代码以便将图像的名称另存为日期时间戳?
Button buttonTakePicture;
final int RESULT_SAVEIMAGE = 0;
/** Called when the activity is first created. */ @Override public
void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera_layout);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
getWindow().setFormat(PixelFormat.UNKNOWN); surfaceView =
(SurfaceView) findViewById(R.id.camerapreview); surfaceHolder =
surfaceView.getHolder(); surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
controlInflater = LayoutInflater.from(getBaseContext()); View
viewControl = controlInflater.inflate(R.layout.camera_control, null);
LayoutParams layoutParamsControl = new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); this.addContentView(viewControl, layoutParamsControl);
buttonTakePicture = (Button) findViewById(R.id.takepicture);
buttonTakePicture.setOnClickListener(new Button.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
camera.takePicture(myShutterCallback, myPictureCallback_RAW,
myPictureCallback_JPG); } });
LinearLayout layoutBackground = (LinearLayout)
findViewById(R.id.background);
layoutBackground.setOnClickListener(new
LinearLayout.OnClickListener() { public void onClick(View arg0) {
// TODO Auto-generated method stub
buttonTakePicture.setEnabled(false);
camera.autoFocus(myAutoFocusCallback); } }); }
AutoFocusCallback myAutoFocusCallback = new AutoFocusCallback() {
public void onAutoFocus(boolean arg0, Camera arg1) { // TODO
Auto-generated method stub buttonTakePicture.setEnabled(true); }
};
ShutterCallback myShutterCallback = new ShutterCallback() { public
void onShutter() { // TODO Auto-generated method stub
} };
PictureCallback myPictureCallback_RAW = new PictureCallback() {
public void onPictureTaken(byte[] arg0, Camera arg1) { // TODO
Auto-generated method stub
} };
PictureCallback myPictureCallback_JPG = new PictureCallback() {
public void onPictureTaken(byte[] arg0, Camera arg1) { // TODO
Auto-generated method stub /*
* Bitmap bitmapPicture = BitmapFactory.decodeByteArray(arg0, 0,
* arg0.length);
*/
Uri uriTarget = getContentResolver().insert(
Media.EXTERNAL_CONTENT_URI, new ContentValues()); OutputStream imageFileOS; try {
imageFileOS = getContentResolver().openOutputStream(uriTarget);
imageFileOS.write(arg0);
imageFileOS.flush();
imageFileOS.close();
Toast.makeText(CameraActivity.this,"Image saved: " + uriTarget.toString(),Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace(); } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace(); } camera.startPreview(); } };
public void surfaceChanged(SurfaceHolder holder, int format, int
width,int height) { // TODO Auto-generated method stub if
(previewing) { camera.stopPreview(); previewing = false; }
if (camera != null) { try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
previewing = true; } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace(); } } }
public void surfaceCreated(SurfaceHolder holder) { // TODO
Auto-generated method stub camera = Camera.open(); }
public void surfaceDestroyed(SurfaceHolder holder) { // TODO
Auto-generated method stub camera.stopPreview(); camera.release();
camera = null; previewing = false; } }
答案 0 :(得分:0)
我认为您可以使用内容解析器来实现它,
ContentValues v = new ContentValues();
v.put(Images.Media.TITLE, title);
v.put(Images.Media.DISPLAY_NAME, System..currenttimemillis());
v.put(Images.Media.DESCRIPTION, description);
v.put(Images.Media.DATE_ADDED, dateTaken);
v.put(Images.Media.DATE_TAKEN, dateTaken);
v.put(Images.Media.DATE_MODIFIED, dateTaken) ;
v.put(Images.Media.MIME_TYPE, “image/jpeg”);
v.put(Images.Media.ORIENTATION, orientation);
试一试。
答案 1 :(得分:0)
这可能会有所帮助
private void SaveImage(Bitmap finalBitmap, String retailerArray2) {
long timeStamp = Calendar.getInstance().getTimeInMillis();
String dateStamp = DateHelper.dateTime(timeStamp,
"dd-MM-yyyy HH:mm[:ss]");
File myDirectory = new File(root + "/ Gallery/" );
if (!myDirectory.exists()) {
myDirectory.mkdirs();
}
String fname = Name + " -- " + dateStamp + ".jpg";
File file = new File(myDirectory, fname);
}