My custom camera apps crash if called by another application.
Like for example, my camera was called by my tracks. If you press the camera icon of my tracks, it will popup and let the user select what camera want to use. I have tried to use my camera but it crashes. But if I click the camera from the application menu, my camera works fine.
AndroidManifest.xml
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<application android:theme="@style/AppTheme"
android:label="@string/app_name"
android:icon="@drawable/ic_launcher"
android:allowBackup="true">
<activity android:name=".CameraActivity" android:clearTaskOnLaunch="false">
<intent-filter>
<action android:name="android.media.action.IMAGE_CAPTURE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:label="@string/app_name"
android:name="com.joules.prdpcamera.GPSInitialize"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:label="@string/app_name"
android:name="com.joules.prdpcamera.MainActivity"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation">
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.MAINACTIVITY" />
</intent-filter>
</activity>
</application>
My onCreate()
method :
protected void onCreate(Bundle paramBundle)
{
super.onCreate(paramBundle);
setContentView(R.layout.gpsinitial);
this.textLat = ((TextView)findViewById(R.id.vLatitude));
this.textLong = ((TextView)findViewById(R.id.vLongitude));
this.txtIni = ((TextView)findViewById(R.id.textView1));
this.marginError = ((TextView)findViewById(R.id.vME));
this.tvview = ((TextView)findViewById(R.id.tvView));
this.btnCam = ((Button)findViewById(R.id.btnCamera));
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener ll = new myLocationListener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
}
LogCat:
03-04 08:32:02.049: I/Choreographer(3273): Skipped 56 frames! The application may be doing too much work on its main thread.
03-04 08:32:02.479: D/gralloc_goldfish(3273): Emulator without GPU emulation detected.
03-04 08:32:13.809: D/dalvikvm(3273): GC_FOR_ALLOC freed 127K, 7% free 2970K/3164K, paused 222ms, total 223ms
03-04 08:32:13.819: I/dalvikvm-heap(3273): Grow heap (frag case) to 4.133MB for 1228816-byte allocation
03-04 08:32:14.089: D/dalvikvm(3273): GC_FOR_ALLOC freed <1K, 5% free 4170K/4368K, paused 235ms, total 235ms
03-04 08:32:14.339: D/dalvikvm(3273): GC_FOR_ALLOC freed <1K, 5% free 4170K/4368K, paused 143ms, total 144ms
03-04 08:32:14.489: I/dalvikvm-heap(3273): Grow heap (frag case) to 5.304MB for 1228816-byte allocation
03-04 08:32:14.629: D/dalvikvm(3273): GC_FOR_ALLOC freed <1K, 4% free 5370K/5572K, paused 138ms, total 138ms
03-04 08:32:16.269: D/dalvikvm(3273): GC_FOR_ALLOC freed 1200K, 25% free 4254K/5656K, paused 256ms, total 257ms
03-04 08:32:16.349: I/dalvikvm-heap(3273): Grow heap (frag case) to 6.471MB for 2366704-byte allocation
03-04 08:32:17.609: D/dalvikvm(3273): GC_FOR_ALLOC freed <1K, 18% free 6565K/7968K, paused 226ms, total 227ms
03-04 08:32:18.529: D/AndroidRuntime(3273): Shutting down VM
03-04 08:32:18.529: W/dalvikvm(3273): threadid=1: thread exiting with uncaught exception (group=0xb3adfba8)
03-04 08:32:18.549: E/AndroidRuntime(3273): FATAL EXCEPTION: main
03-04 08:32:18.549: E/AndroidRuntime(3273): Process: com.joules.prdpcamera, PID: 3273
03-04 08:32:18.549: E/AndroidRuntime(3273): java.lang.NullPointerException
03-04 08:32:18.549: E/AndroidRuntime(3273): at android.media.ExifInterface.saveAttributes(ExifInterface.java:265)
03-04 08:32:18.549: E/AndroidRuntime(3273): at com.joules.prdpcamera.MainActivity.UpdateGeoTag(MainActivity.java:635)
03-04 08:32:18.549: E/AndroidRuntime(3273): at com.joules.prdpcamera.MainActivity$4.onPictureTaken(MainActivity.java:590)
03-04 08:32:18.549: E/AndroidRuntime(3273): at android.hardware.Camera$EventHandler.handleMessage(Camera.java:917)
03-04 08:32:18.549: E/AndroidRuntime(3273): at android.os.Handler.dispatchMessage(Handler.java:102)
03-04 08:32:18.549: E/AndroidRuntime(3273): at android.os.Looper.loop(Looper.java:136)
03-04 08:32:18.549: E/AndroidRuntime(3273): at android.app.ActivityThread.main(ActivityThread.java:5017)
03-04 08:32:18.549: E/AndroidRuntime(3273): at java.lang.reflect.Method.invokeNative(Native Method)
03-04 08:32:18.549: E/AndroidRuntime(3273): at java.lang.reflect.Method.invoke(Method.java:515)
03-04 08:32:18.549: E/AndroidRuntime(3273): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-04 08:32:18.549: E/AndroidRuntime(3273): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-04 08:32:18.549: E/AndroidRuntime(3273): at dalvik.system.NativeStart.main(Native Method)
03-04 08:32:28.839: I/Process(3273): Sending signal. PID: 3273 SIG: 9
03-04 08:33:05.939: D/dalvikvm(3327): GC_FOR_ALLOC freed 35K, 4% free 2852K/2956K, paused 48ms, total 51ms
03-04 08:33:05.989: I/dalvikvm-heap(3327): Grow heap (frag case) to 6.385MB for 3712464-byte allocation
03-04 08:33:06.029: D/dalvikvm(3327): GC_FOR_ALLOC freed 2K, 2% free 6474K/6584K, paused 39ms, total 39ms
03-04 08:33:06.839: D/gralloc_goldfish(3327): Emulator without GPU emulation detected.
03-04 08:33:26.809: I/Choreographer(3327): Skipped 144 frames! The application may be doing too much work on its main thread.
03-04 08:33:35.429: D/dalvikvm(3327): GC_FOR_ALLOC freed 87K, 3% free 6765K/6920K, paused 220ms, total 223ms
03-04 08:33:36.019: D/dalvikvm(3327): GC_FOR_ALLOC freed 4K, 3% free 7961K/8124K, paused 188ms, total 189ms
03-04 08:33:37.609: D/dalvikvm(3327): GC_FOR_ALLOC freed 1200K, 15% free 8045K/9412K, paused 220ms, total 222ms
03-04 08:33:37.729: I/dalvikvm-heap(3327): Grow heap (frag case) to 10.173MB for 2366704-byte allocation
03-04 08:33:38.119: D/dalvikvm(3327): GC_FOR_ALLOC freed <1K, 12% free 10356K/11724K, paused 44ms, total 45ms
03-04 08:33:38.719: D/AndroidRuntime(3327): Shutting down VM
03-04 08:33:38.719: W/dalvikvm(3327): threadid=1: thread exiting with uncaught exception (group=0xb3adfba8)
03-04 08:33:38.729: E/AndroidRuntime(3327): FATAL EXCEPTION: main
03-04 08:33:38.729: E/AndroidRuntime(3327): Process: com.joules.prdpcamera, PID: 3327
03-04 08:33:38.729: E/AndroidRuntime(3327): java.lang.NullPointerException
03-04 08:33:38.729: E/AndroidRuntime(3327): at android.media.ExifInterface.saveAttributes(ExifInterface.java:265)
03-04 08:33:38.729: E/AndroidRuntime(3327): at com.joules.prdpcamera.MainActivity.UpdateGeoTag(MainActivity.java:635)
03-04 08:33:38.729: E/AndroidRuntime(3327): at com.joules.prdpcamera.MainActivity$4.onPictureTaken(MainActivity.java:590)
03-04 08:33:38.729: E/AndroidRuntime(3327): at android.hardware.Camera$EventHandler.handleMessage(Camera.java:917)
03-04 08:33:38.729: E/AndroidRuntime(3327): at android.os.Handler.dispatchMessage(Handler.java:102)
03-04 08:33:38.729: E/AndroidRuntime(3327): at android.os.Looper.loop(Looper.java:136)
03-04 08:33:38.729: E/AndroidRuntime(3327): at android.app.ActivityThread.main(ActivityThread.java:5017)
03-04 08:33:38.729: E/AndroidRuntime(3327): at java.lang.reflect.Method.invokeNative(Native Method)
03-04 08:33:38.729: E/AndroidRuntime(3327): at java.lang.reflect.Method.invoke(Method.java:515)
03-04 08:33:38.729: E/AndroidRuntime(3327): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-04 08:33:38.729: E/AndroidRuntime(3327): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-04 08:33:38.729: E/AndroidRuntime(3327): at dalvik.system.NativeStart.main(Native Method)
03-04 08:33:55.149: I/Process(3327): Sending signal. PID: 3327 SIG: 9
03-04 08:33:58.609: D/dalvikvm(3361): GC_FOR_ALLOC freed 39K, 4% free 2852K/2960K, paused 58ms, total 62ms
03-04 08:33:58.659: I/dalvikvm-heap(3361): Grow heap (frag case) to 6.385MB for 3712464-byte allocation
03-04 08:33:58.719: D/dalvikvm(3361): GC_FOR_ALLOC freed 2K, 2% free 6475K/6588K, paused 51ms, total 51ms
03-04 08:33:59.899: D/gralloc_goldfish(3361): Emulator without GPU emulation detected.
Camera activity:
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "Storing image... Please wait...", Toast.LENGTH_LONG).show();
Parameters params = cam.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_AUTO);
Camera.Parameters parameters = cam.getParameters();
params.set("orientation", "portrait");
cam.setParameters(params);
cam.takePicture(shutterCallback, null, jpegCallback);
}
});
SurfaceView surface = (SurfaceView)findViewById(R.id.surfaceview);
SurfaceHolder holder = surface.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
holder.setFixedSize(400, 300);
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener ll = new myLocationListener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
//vlatitude = df.format(clat);
//vlongitude = df.format(clng);
}
This is my launcher which is called first when my apps start:
public class GPSInitialize extends Activity {
public static final String tag = null;
Button btnCam;
TextView marginError;
TextView textLat;
TextView textLong;
TextView tvview;
TextView txtIni;
public void onBackPressed()
{
super.onBackPressed();
finish();
System.exit(0);
}
protected void onCreate(Bundle paramBundle)
{
super.onCreate(paramBundle);
setContentView(R.layout.gpsinitial);
this.textLat = ((TextView)findViewById(R.id.vLatitude));
this.textLong = ((TextView)findViewById(R.id.vLongitude));
this.txtIni = ((TextView)findViewById(R.id.textView1));
this.marginError = ((TextView)findViewById(R.id.vME));
this.tvview = ((TextView)findViewById(R.id.tvView));
this.btnCam = ((Button)findViewById(R.id.btnCamera));
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener ll = new myLocationListener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
}
public boolean onCreateOptionsMenu(Menu paramMenu)
{
getMenuInflater().inflate(2131165184, paramMenu);
return true;
}
protected void onResume()
{
super.onResume();
}
public void startCamera(View paramView)
{
startActivity(new Intent(this, MainActivity.class));
}
private class myLocationListener implements LocationListener
{
private myLocationListener()
{
}
public void onLocationChanged(Location paramLocation)
{
if (paramLocation != null)
{
double d1 = paramLocation.getLatitude();
double d2 = paramLocation.getLongitude();
float f = paramLocation.getAccuracy();
GPSInitialize.this.textLat.setText(Double.toString(d1));
GPSInitialize.this.textLong.setText(Double.toString(d2));
GPSInitialize.this.txtIni.setText("GPS Locked");
GPSInitialize.this.tvview.setText("Coordinates found");
GPSInitialize.this.marginError.setText(Float.toString(f));
PSInitialize.this.btnCam.setEnabled(true);
}
}
public void onProviderDisabled(String paramString)
{
Log.v(GPSInitialize.tag, "Your GPS is not currently turn ON");
Toast.makeText(GPSInitialize.this, "GPS not found. Please turn ON your GPS", 1).show();
Intent localIntent = new Intent("android.settings.LOCATION_SOURCE_SETTINGS");
GPSInitialize.this.startActivity(localIntent);
}
public void onProviderEnabled(String paramString)
{
Toast.makeText(GPSInitialize.this, "GPS found. Initializing GPS", 1).show();
}
public void onStatusChanged(String paramString, int paramInt, Bundle paramBundle)
{
}
}
}
Some part of my main activity
public class MainActivity extends Activity implements SurfaceHolder.Callback {
protected static final String TAG = null;
private boolean focusing;
private Camera cam;
SurfaceHolder holder;
private boolean isDoubleBackPressd = false;
String vlatitude;
String vlongitude;
String filename;
String timeStamp;
String flat;
String flong;
String latref;
String longref;
String outbar;
String outbar2;
String digitake;
String datePic;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
LinearLayout l = new LinearLayout(this);
l.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
l.setOrientation(LinearLayout.VERTICAL);
ImageButton b=(ImageButton) findViewById(R.id.Button1);
b.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "Storing image... Please wait...", Toast.LENGTH_LONG).show(); Parameters params = cam.getParameters(); params.setFlashMode(Parameters.FLASH_MODE_AUTO); Camera.Parameters parameters = cam.getParameters();
params.set("orientation", "portrait");
cam.setParameters(params);
cam.takePicture(shutterCallback, null, jpegCallback);
}
});
SurfaceView surface = (SurfaceView)findViewById(R.id.surfaceview);
SurfaceHolder holder = surface.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
holder.setFixedSize(400, 300);
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener ll = new myLocationListener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
//vlatitude = df.format(clat);
//vlongitude = df.format(clng);
}
private static final int WHITE = 0xFFFFFFFF;
private static final int BLACK = 0xFF000000;
private class myLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
if(location !=null){
double clat=location.getLatitude();
double clng=location.getLongitude();
DecimalFormat df = new DecimalFormat("###.######");
vlatitude = df.format(clat);
vlongitude = df.format(clng);
int num1Lat = (int)Math.floor(clat);
int num2Lat = (int)Math.floor((clat - num1Lat) * 60);
double num3Lat = (clat - ((double)num1Lat+((double)num2Lat/60))) * 3600000;
int num1Lon = (int)Math.floor(clng);
int num2Lon = (int)Math.floor((clng - num1Lon) * 60);
double num3Lon = (clng - ((double)num1Lon+((double)num2Lon/60))) * 3600000;
flat = num1Lat+"/1,"+num2Lat+"/1,"+num3Lat+"/1000";
flong = num1Lon+"/1,"+num2Lon+"/1,"+num3Lon+"/1000";
//edit: vlatitude, vlongitude, flat, flong, outbar, outbar2
if (clat > 0) {
latref = "N";
} else {
latref = "S";
}
if (clng> 0) {
longref = "E";
} else {
longref = "W";
}
//Toast.makeText(MainActivity.this, "GPS is Excellent", Toast.LENGTH_LONG).show();
}
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
//--------------
}
@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
// TODO Auto-generated method stub
}
}
/*
* initiate auto focus
*/
AutoFocusCallback myAutoFocusCallback = new AutoFocusCallback() {
@Override
public void onAutoFocus(boolean arg0, Camera arg1) {
// TODO Auto-generated method stub
cam.autoFocus(this);
focusing = true;
//Toast.makeText(getApplicationContext(), "Focused", Toast.LENGTH_SHORT).show();
}
};
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
cam.stopPreview();
cam.startPreview();
}
@Override
public void surfaceCreated(SurfaceHolder arg0)
{
// TODO Auto-generated method stub
try
{
Camera.Parameters parameters = cam.getParameters();
if (parameters.getSupportedFocusModes().contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) {
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
cam.autoFocus(new AutoFocusCallback() {
public void onAutoFocus(boolean success, Camera camera) {
//Log.d(TAG, “AutoFocus: “ + (success ? “Succeeded” : “Failed”));
//cam.autoFocus(myAutoFocusCallback);
}
});
cam.setPreviewCallback(new PreviewCallback()
{
@Override
public void onPreviewFrame(byte[] data, Camera camera)
{
int quality = 100;
Size previewSize = camera.getParameters().getPreviewSize();
YuvImage image = new YuvImage(data, ImageFormat.NV21,previewSize.width, previewSize.height, null);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
image.compressToJpeg(new Rect(0, 0,previewSize.width, previewSize.height),quality, outputStream);
}
});
Timer tim=new Timer();
tim.schedule(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
cam.startPreview();
}
}, 3000);
}
cam.setPreviewDisplay(arg0);
cam.setDisplayOrientation(90);
cam.startPreview();
// TODO Draw over the preview if required.
} catch (IOException e)
{
//Log.d(TAG, “IO Exception”, e);
}
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
// TODO Auto-generated method stub
Parameters params = cam.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_OFF);
holder.removeCallback(this);
cam.stopPreview();
cam.release();
focusing = false;
cam=null;
}