I have written this code for android 1.6 and it works fine on the emulator.
but when i tested it on actual device.it crashes after taking a photo.
i m not getting what could be the issue.
plz help me
my code is as follows
Cam.java
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.MediaStore.Images.Media;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class Cam extends Activity implements SurfaceHolder.Callback,
LocationListener {
ProgressDialog myProgressDialog = null;
public static String filename = null;
private Camera camera;
private boolean isPreviewRunning = false;
private SimpleDateFormat timeStampFormat = new SimpleDateFormat(
"yyyyMMddHHmmssSS");
private SurfaceView surfaceView;
private SurfaceHolder surfaceHolder;
private Uri target = Media.EXTERNAL_CONTENT_URI;
private Button btnCapture;
private LocationManager mgr;
private String best;
MediaScannerConnection mConnection;
public static String result = null;
ImageCaptureCallback iccb = null;
Main imgCap = new Main();
private String locInfo = null;
Context context;
Cursor cursor;
int column_index;
public int i = 0;
public static double lon, lat;
public static String gpstype;
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
Log.e(getClass().getSimpleName(), "onCreate");
getWindow().setFormat(PixelFormat.TRANSLUCENT);
setContentView(R.layout.imagecap);
mgr = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
best = mgr.getBestProvider(criteria, true);
Location location = mgr.getLastKnownLocation(best);
if (location == null) {
locInfo = "Failed to locate GPS co-ordinates!";
}
AlertDialog ad = new AlertDialog.Builder(Cam.this).create();
ad.setMessage(locInfo);
ad.setTitle("GPS Status");
ad.setButton("Continue", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
return;
}
});
ad.show();
this.btnCapture = (Button) this.findViewById(R.id.btnCapture);
surfaceView = (SurfaceView) findViewById(R.id.surface);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
btnCapture.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
ImageCaptureCallback iccb = null;
filename = timeStampFormat.format(new Date());
ContentValues values = new ContentValues();
values.put(Media.TITLE, filename);
values.put(Media.DESCRIPTION,
"Image from Android");
Uri uri = getContentResolver().insert(
Media.EXTERNAL_CONTENT_URI, values);
iccb = new ImageCaptureCallback(getContentResolver()
.openOutputStream(uri));
camera.takePicture(mShutterCallback, mPictureCallbackJpeg,
iccb);
String[] proj = { MediaStore.Images.Media.DATA };
final Cursor cursor = managedQuery(uri, proj, null, null,
null);
column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
sendBroadcast(new Intent(
Intent.ACTION_MEDIA_MOUNTED,
Uri
.parse("file://"
+ Environment
.getExternalStorageDirectory())));
Location location = mgr.getLastKnownLocation(best);
if (location == null) {
locInfo = "";
} else {
locInfo = "Image Saved..!\n\nLongitude = "
+ location.getLongitude();
locInfo += "\nLatitude = " + location.getLatitude();
locInfo += "\nProvider = " + location.getProvider();
lon = location.getLongitude();
lat = location.getLatitude();
gpstype = location.getProvider();
}
myProgressDialog = ProgressDialog.show(Cam.this,
"Processing", "Please Wait", true);
myProgressDialog.setCancelable(false);
new Thread() {
public void run() {
try {
Intent ii = new Intent(Cam.this, Gal.class);
startActivity(ii);
} catch (Exception e) {
e.printStackTrace();
}
myProgressDialog.dismiss();
}
}.start();
} catch (Exception ex) {
ex.printStackTrace();
Log.e(getClass().getSimpleName(), ex.getMessage(), ex);
Toast.makeText(Cam.this, ex.getMessage(),
Toast.LENGTH_SHORT).show();
}
camera.startPreview();
isPreviewRunning = true;
}
});
}
@Override
protected void onPause() {
super.onPause();
mgr.removeUpdates(this);
}
public boolean onCreateOptionsMenu(android.view.Menu menu) {
MenuItem item = menu.add(0, 0, 0, "goto gallery");
item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Intent intent = new Intent(Intent.ACTION_VIEW, target);
startActivity(intent);
return true;
}
});
return true;
}
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
}
Camera.PictureCallback mPictureCallbackRaw = new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera c) {
Log.e(getClass().getSimpleName(), "PICTURE CALLBACK RAW: " + data);
camera.startPreview();
}
};
Camera.PictureCallback mPictureCallbackJpeg = new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera c) {
Log.e(getClass().getSimpleName(),
"PICTURE CALLBACK JPEG: data.length = " + data);
}
};
Camera.ShutterCallback mShutterCallback = new Camera.ShutterCallback() {
public void onShutter() {
Log.e(getClass().getSimpleName(), "SHUTTER CALLBACK");
}
};
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
}
if (keyCode == KeyEvent.KEYCODE_BACK) {
return super.onKeyDown(keyCode, event);
}
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
camera.takePicture(mShutterCallback, mPictureCallbackJpeg, iccb);
// Toast.makeText(Cam.this, "Image Saved..!", Toast.LENGTH_SHORT)
// .show();
return true;
}
return false;
}
protected void onResume() {
// Log.e(getClass().getSimpleName(), "onResume");
super.onResume();
mgr.requestLocationUpdates(best, 15000, 1, this);
}
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
protected void onStop() {
// Log.e(getClass().getSimpleName(), "onStop");
super.onStop();
}
public void surfaceCreated(SurfaceHolder holder) {
// Log.e(getClass().getSimpleName(), "surfaceCreated");
camera = Camera.open();
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// Log.e(getClass().getSimpleName(), "surfaceChanged");
if (isPreviewRunning) {
camera.stopPreview();
}
Camera.Parameters p = camera.getParameters();
p.setPreviewSize(w, h);
camera.setParameters(p);
try {
camera.setPreviewDisplay(holder);
} catch (IOException e) {
new AlertDialog.Builder(this).setMessage("Failed to fetch Camera preview!").show();
e.printStackTrace();
}
camera.startPreview();
isPreviewRunning = true;
}
public void surfaceDestroyed(SurfaceHolder holder) {
// Log.e(getClass().getSimpleName(), "surfaceDestroyed");
camera.stopPreview();
isPreviewRunning = false;
camera.release();
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
ImageCaptureCallback.java
package org.LoyalPuppy;
import java.io.OutputStream;
import org.kobjects.base64.Base64;
import android.app.Activity;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
public class ImageCaptureCallback extends Activity implements PictureCallback {
private OutputStream filoutputStream;
public static String img;
public ImageCaptureCallback(OutputStream filoutputStream) {
this.filoutputStream = filoutputStream;
}
public void onPictureTaken(byte[] data, Camera camera) {
try {
filoutputStream.write(data);
filoutputStream.flush();
filoutputStream.close();
img = Base64.encode(data);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
what could be the problem
plz hlp me..
thnx in advance...


