Tuesday, June 1, 2010

[android-developers] Strange Problem with 1.5 Image Saving...Please Help

Hi everyone,

I have created an activity which uses the camera to take a picture and
save that image to the sd card. This works perfectly on 2.0+ but on a
1.5 phone the image that gets saved is simply a black box. I can not
figure out why the image is coming back pure black. I have pasted the
code below hopefully someone will be able to point out my issue.

public class TakePictureCupCake extends Activity {
private SurfaceView preview=null;
private SurfaceHolder previewHolder=null;
private Camera camera=null;
boolean bIsPictureTaking;
boolean bIsAutoFocusStarted = false;

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

preview=(SurfaceView)findViewById(R.id.preview);
previewHolder=preview.getHolder();
previewHolder.addCallback(surfaceCallback);
previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

preview.setOnClickListener(new OnClickListener(){


public void onClick(View v) {
if(!bIsPictureTaking){
AutoFocusCallBackImpl autoFocusCallBack = new
AutoFocusCallBackImpl();
camera.autoFocus(autoFocusCallBack);
bIsAutoFocusStarted = true;
takePicture();
}

}
});
Context context = getApplicationContext();
CharSequence message = "Click any place on the screen to take your
picture";
int duration = Toast.LENGTH_LONG;
Toast messages = Toast.makeText(context, message, duration);
messages.show();
}

private class AutoFocusCallBackImpl implements
Camera.AutoFocusCallback{
public void onAutoFocus(boolean success, Camera camera){

}
}


private void takePicture() {
bIsPictureTaking = true;
bIsAutoFocusStarted=false;
camera.takePicture(null, null, photoCallback);
}

SurfaceHolder.Callback surfaceCallback=new SurfaceHolder.Callback() {
public void surfaceCreated(SurfaceHolder holder) {
camera=Camera.open();

try {
camera.setPreviewDisplay(previewHolder);

}
catch (Throwable t) {
Log.e("Error","Error with Display", t);
Toast.makeText(TakePictureCupCake.this, t.getMessage(),
Toast.LENGTH_LONG).show();
}
}

public void surfaceChanged(SurfaceHolder holder,int format, int
width,int height) {
Camera.Parameters parameters=camera.getParameters();

parameters.setPreviewSize(width, height);
parameters.setPictureFormat(PixelFormat.JPEG);

//parameters.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);


camera.setParameters(parameters);
camera.startPreview();
}

public void surfaceDestroyed(SurfaceHolder holder) {
camera.stopPreview();
camera.release();
camera=null;
}
};

PictureCallback photoCallback=new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {


new SavePhotoTask().execute(data);
camera.startPreview();

}
};

class SavePhotoTask extends AsyncTask<byte[], String, String> {
@Override
protected String doInBackground(byte[]... jpeg) {
File photoDirectory=new
File(Environment.getExternalStorageDirectory(),"fishdog");
Long currTime = Calendar.getInstance().getTimeInMillis();
String sCurrTime = currTime.toString();
currTime = null;

File photo = new File(photoDirectory,"fishdogpic" + sCurrTime
+".jpg");


try {

if(photoDirectory.exists()==true){
FileOutputStream fos=new FileOutputStream(photo.getPath());
fos.write(jpeg[0]);
fos.close();
setFilePath(photoDirectory + "/fishdogpic" + sCurrTime + ".jpg");
fos=null;
//camera.stopPreview();
//camera.release();
//camera=null;

}else{
photoDirectory.mkdir();
FileOutputStream fos=new FileOutputStream(photo.getPath());
fos.write(jpeg[0]);
fos.close();
setFilePath(photoDirectory + "/fishdogpic" + sCurrTime +
".jpg");
fos=null;
//camera.stopPreview();
//camera.release();
//camera=null;
}
photoDirectory=null;
photo=null;
sCurrTime = null;

//finish();

}
catch (java.io.IOException e) {
Log.e("Error", "Error with Picture Captured", e);
}

return(null);
}
}
private void setFilePath(String fn){
((xmlName)this.getApplication()).setLogPicFileName(fn);
}
}


Thanks so much for your time and advice!

Tommy

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment