views:

4092

answers:

4

I have run into a small issue with something that I am probably just overlooking.

I want to take a picture from the surface preview of the camera, and save it to the sd_card. This works ALMOST perfectly. I assign it a file name, and for one reason or another, it does not use the filename.

This is what I have been trying to do :

    Button imagecapture = (Button)findViewById(R.id.imagecapture);
 imagecapture.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
         String filename = null;
         ImageCaptureCallback iccb = null;

          try {
          filename = timeStampFormat.format(new Date());
          ContentValues values = new ContentValues();
          values.put(Media.TITLE, filename);
          values.put(Media.DESCRIPTION, "Image capture by camera");

          Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
          iccb = new ImageCaptureCallback( getContentResolver().openOutputStream(uri));
          } catch(Exception ex ){
          ex.printStackTrace();
          Log.e(getClass().getSimpleName(), ex.getMessage(), ex);
          }
         camera.takePicture(mShutterCallback, mPictureCallbackRaw, iccb);
         com.froogloid.android.gspot.Park.imageFileName = filename;


        }
    });

It won't use the filename (i.e. time/date stamp I ask it to.)

+2  A: 

This was resolved by implementing PictureCallback via a ImageCaptureCallback class, and Overriding the onPictureTaken where the file was being written via a file output stream. All you had to do was change the fileoutput stream to the filename you want.

Chrispix
A: 

it doesn't save image? in my app this code saves image, maybe you use variable "filaname" to get image from sdcard? to use image from sdcard is better to save in variable, for examople, "fileUri" value of uri.toString, end get from sdcard file with uri Uri.parse(fileUri)..

It was saving the image fine, I was trying this :ContentValues values = new ContentValues(); values.put(Media.TITLE, filename);Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);That did not work for me. I went about it the best way I could
Chrispix
A: 

can u tell me how to do this...that is change the name in the PictureCallback via a ImageCaptureCallback class,

Hopefully the code below helps.
Chrispix
A: 

Here you go : Hope this helps / Probably not the best way to go about it, but it worked.

/////////////////////////////////////////////////////////////////////////////// // // This is the camera capture image callback. //
// // //////////////////////////////////////////////////////////////////////////////

import java.io.FileOutputStream; import java.io.OutputStream;

import android.hardware.Camera; import android.hardware.Camera.PictureCallback; import android.util.Log; import android.widget.Toast;

public class ImageCaptureCallback implements PictureCallback {

private OutputStream filoutputStream;
public ImageCaptureCallback(OutputStream filoutputStream) {

 this.filoutputStream = filoutputStream;



}
@Override  
public void onPictureTaken(byte[] data, Camera camera) {

 try {
   Log.v(getClass().getSimpleName(), "onPictureTaken=" + data + " length = " + data.length);


      FileOutputStream buf = new FileOutputStream("/sdcard/dcim/Camera/" + CameraActivity.filename + ".jpg");
   buf.write(data);
   buf.flush();
   buf.close();
    // filoutputStream.write(data);
   filoutputStream.flush();
   filoutputStream.close();

  } catch(Exception ex) {
   ex.printStackTrace();
  }
  }

}
Chrispix