I'm trying to realise a custom camera app.
My aim is to add on the taken pic text like GPS data. Actually, I've two problems. The first occured randomly, my app stops working without error message. When I take a pic, the byte array (data of the pic ) is sending by intent.putExtras() to another activity. This second activity allows the user to see his pic, in the future to add text like GPS data to it, to cancel it or to save it.
My app stops working just before showing this second activity. Sometimes after 0 pic, often after 2-3 pics taken (and for these 2-3 pics, my app has worked correctly). I can't understand it.
The second problem is not the question, but if somebody has the answser : I'm not able to write text on my bitmap, when I try this answser (https://stackoverflow.com/a/7328777/9917854) my app also stops working without message...
Code : MainActivty onCreate()
private CameraPreview mPreview;
//...
if (checkCameraHardware(this)) {
mCamera = getCameraInstance();
mPreview = new CameraPreview(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mPreview);
Button captureButton = (Button) findViewById(R.id.button_capture);
captureButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
a=0;
Intent intent = new Intent("com.reboulnoah.picloc");
intent.putExtra("value", 1);
sendBroadcast(intent);
mCamera.takePicture(null, null, mPicture);
}
}
);
}
}
MainActivity getCameraInstance ()
public static Camera getCameraInstance() {
Camera c = null;
try {
c = Camera.open(); // attempt to get a Camera instance
} catch (Exception e) {
Log.e("APP", e.getMessage());
}
return c;
}
MainActivity onPicTaken()
private Camera.PictureCallback mPicture = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
mCamera.stopPreview();
mCamera.startPreview();
Intent i = new Intent(MainActivity.this, CustomizePic.class);
i.putExtra("pic", data);
startActivity(i);
}
};
CameraPreview is a class extracted from Android Developper official website. I've changed to things but my app stopped before too. I've changed surfaceDestroyed. There was no code in, and in surfaceCreated, I've rotate the camera.
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
private static final String TAG = "MyActivity";
public CameraPreview(Context context, Camera camera) {
super(context);
mCamera = camera;
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
try {
mCamera.setPreviewDisplay(holder);
mCamera.setDisplayOrientation(90);
Camera.Parameters parameters = mCamera.getParameters();
parameters.setRotation(90);
mCamera.setParameters(parameters);
mCamera.startPreview();
} catch (IOException e) {
Log.e(TAG, "surfaceCreated "+ e.getMessage());
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
mCamera.stopPreview();
Log.e(TAG,"surfaceDEst");
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
if (mHolder.getSurface() == null){
return;
}
try {
mCamera.stopPreview();
} catch (Exception e){
// ignore: tried to stop a non-existent preview
}
// set preview size and make any resize, rotate or
// reformatting changes here
// start preview with new settings
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (Exception e){
Log.e(TAG, "SurfaceChanged "+e.getMessage());
}
}
My second Activity CustomizePic onCreate()
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
final byte[] data = getIntent().getByteArrayExtra("pic");
if(data != null){
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
LinearLayout linearLayout = findViewById(R.id.linear2);
SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
String restoredText = prefs.getString("alt", null);
Drawable d = new BitmapDrawable(bitmap);
linearLayout.setBackground(d);
}
So what I'm doing wrong ? Why my app stops after 2-3 pic when I take a pic ?
Thank you