Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Android camera activity take picture freezes
- package org.my.activities;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import org.mokus.vilniaus.energija.R;
- import org.mokus.vilniaus.energija.database.HeatAdapter;
- import org.mokus.vilniaus.energija.dialogs.SDCardDialog;
- import org.mokus.vilniaus.energija.dialogs.StatusDialog;
- import org.mokus.vilniaus.energija.entities.EntHeat;
- import org.mokus.vilniaus.energija.helper.AppValues;
- import org.mokus.vilniaus.energija.helper.Constants;
- import org.mokus.vilniaus.energija.helper.Extras;
- import org.mokus.vilniaus.energija.helper.WebClient;
- import org.mokus.vilniaus.energija.layout.MeterAppSurfaceView;
- import android.app.Activity;
- import android.app.ProgressDialog;
- import android.content.Context;
- import android.content.Intent;
- import android.hardware.Camera;
- import android.hardware.Camera.AutoFocusCallback;
- import android.hardware.Camera.Parameters;
- import android.media.AudioManager;
- import android.media.MediaPlayer;
- import android.net.Uri;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.os.Environment;
- import android.util.Log;
- import android.view.View;
- import android.view.Window;
- import android.widget.Button;
- import android.widget.FrameLayout;
- import android.widget.RelativeLayout;
- import android.widget.TextView;
- import com.google.gson.Gson;
- public class TakePhotoActivity extends Activity {
- public Activity a;
- public Button btnSave;
- public Parameters cameraParameters;
- public EntHeat hCounter;
- public String counterType;
- public String counterNumber;
- public String dateTaken;
- public MeterAppSurfaceView svPhoto;
- public Button btnSkip;
- public static int clickCount;
- public static final int PHOTO_COUNT = 3;
- public TextView tvHeader;
- public MyPictureCallBack mpcb;
- @Override
- protected void onCreate(Bundle b) {
- super.onCreate(b);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- setContentView(R.layout.camera_dialog);
- Intent intent = getIntent();
- hCounter = null;
- clickCount = 0;
- try {
- hCounter = (EntHeat) intent.getSerializableExtra(Extras.COUNTER);
- } catch (Exception e) {
- Log.e("TakePhoto.Exception.79", e.getMessage());
- }
- counterType = "siluma_mwh";
- counterNumber = "" + hCounter.id;
- dateTaken = hCounter.dtaken;
- dateTaken = dateTaken.replace(":", "");
- dateTaken = dateTaken.replace("-", "");
- dateTaken = dateTaken.replace(" ", "");
- initControls();
- final File sdCard = Environment.getExternalStorageDirectory();
- if (!sdCard.exists()) {
- new SDCardDialog(this).show();
- }
- btnSave.setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- if (!sdCard.exists()) {
- new SDCardDialog(TakePhotoActivity.this).show();
- return;
- }
- clickCount++;
- btnSkip.setVisibility(View.VISIBLE);
- new AsyncTask<Void, Void, Void>() {
- ProgressDialog pd;
- @Override
- protected void onPreExecute() {
- String title = TakePhotoActivity.this.getResources()
- .getString(R.string.app_name);
- String pleaseWait = TakePhotoActivity.this
- .getResources().getString(R.string.please_wait);
- pd = ProgressDialog.show(TakePhotoActivity.this, title,
- pleaseWait, true, false);
- shootSound();
- }
- @Override
- protected Void doInBackground(Void... params) {
- svPhoto.camera.stopPreview();
- Camera c = svPhoto.camera;
- if (c != null) {
- c.autoFocus(new AutoFocusCallback() {
- public void onAutoFocus(boolean arg0,
- Camera camera) {
- svPhoto.camera.takePicture(null, null,
- null, mpcb);
- }
- });
- } else {
- }
- try {
- new Thread().sleep(2000);
- } catch (InterruptedException e) {
- }
- return null;
- }
- @Override
- protected void onPostExecute(Void result) {
- Log.e("post", "execute");
- pd.dismiss();
- if (clickCount == PHOTO_COUNT) {
- prepareCounter();
- }
- }
- }.execute();
- }
- });
- btnSkip.setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- try {
- svPhoto.camera.stopPreview();
- svPhoto.camera.setPreviewCallback(null);
- svPhoto.camera.setPreviewDisplay(null);
- svPhoto.camera.setPreviewCallbackWithBuffer(null);
- svPhoto.camera.lock();
- svPhoto.camera.release();
- svPhoto = null;
- } catch (Exception e) {
- e.printStackTrace();
- }
- prepareCounter();
- }
- });
- RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl1);
- rl.bringToFront();
- FrameLayout llPrev = (FrameLayout) findViewById(R.id.llPhotoPreview);
- llPrev.bringToFront();
- btnSave.bringToFront();
- if (hCounter.status == 0) {
- btnSkip.setVisibility(View.INVISIBLE);
- }
- mpcb = new MyPictureCallBack(this, svPhoto, counterType, counterNumber,
- dateTaken);
- }
- public void onPause() {
- try {
- svPhoto.camera.stopPreview();
- svPhoto.camera.setPreviewCallback(null);
- svPhoto.camera.setPreviewDisplay(null);
- svPhoto.camera.setPreviewCallbackWithBuffer(null);
- svPhoto.camera.lock();
- svPhoto.camera.release();
- svPhoto = null;
- } catch (Exception e) {
- e.printStackTrace();
- }
- super.onPause();
- }
- public void onResume() {
- super.onResume();
- if (svPhoto == null) {
- svPhoto = new MeterAppSurfaceView(this);
- svPhoto.setCounterTypeAndNumber(counterType, counterNumber,
- dateTaken);
- FrameLayout llPrev = (FrameLayout) findViewById(R.id.llPhotoPreview);
- llPrev.removeAllViews();
- llPrev.addView(svPhoto);
- }
- }
- public void onDestroy() {
- onPause();
- super.onDestroy();
- }
- public void shootSound() {
- AudioManager meng = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
- int volume = meng.getStreamVolume(AudioManager.STREAM_NOTIFICATION);
- MediaPlayer _shootMP = null;
- if (volume != 0) {
- if (_shootMP == null)
- _shootMP = MediaPlayer
- .create(this,
- Uri.parse("file:///system/media/audio/ui/camera_click.ogg"));
- if (_shootMP != null)
- _shootMP.start();
- }
- }
- public void initControls() {
- btnSave = (Button) findViewById(R.id.btnCapturePhoto);
- svPhoto = new MeterAppSurfaceView(this);
- svPhoto.setCounterTypeAndNumber(counterType, counterNumber, dateTaken);
- FrameLayout llPrev = (FrameLayout) findViewById(R.id.llPhotoPreview);
- llPrev.removeAllViews();
- llPrev.addView(svPhoto);
- btnSkip = (Button) findViewById(R.id.imgSkip);
- tvHeader = (TextView) findViewById(R.id.textView1);
- tvHeader.setText(getResources().getString(R.string.txtCaptureCouter)
- + " 1/3");
- }
- public static class MyPictureCallBack implements Camera.PictureCallback {
- public String counterType, counterNumber, dateTaken;
- public MeterAppSurfaceView msv;
- public Context context;
- public MyPictureCallBack(Context context, MeterAppSurfaceView msv,
- String counterType, String counterNumber, String dateTaken) {
- this.counterType = counterType;
- this.counterNumber = counterNumber;
- this.dateTaken = dateTaken;
- this.msv = msv;
- this.context = context;
- }
- public void onPictureTaken(final byte[] data, final Camera camera) {
- new PicSaver(context, msv, counterType, counterNumber, dateTaken)
- .execute(data);
- }
- }
- public static class PicSaver extends AsyncTask<byte[], Void, Void> {
- public String counterType, counterNumber, dateTaken;
- public MeterAppSurfaceView msv;
- public Context context;
- public PicSaver(Context context, MeterAppSurfaceView msv, String cType,
- String cNumber, String dTaken) {
- Log.e("PicSaver", "create");
- counterType = cType;
- counterNumber = cNumber;
- dateTaken = dTaken;
- this.msv = msv;
- this.context = context;
- }
- @Override
- protected Void doInBackground(byte[]... params) {
- Log.e("savePhoto", "doInBackground");
- String fileName = "/org_Vilniaus_Energija/Vilniaus_Energija_"
- + counterType + "_" + counterNumber + "_" + dateTaken + "_"
- + clickCount + ".jpg";
- FileOutputStream outStream = null;
- try {
- File _f = new File(Environment.getExternalStorageDirectory()
- .toString() + "/org_Vilniaus_Energija");
- if (!_f.exists()) {
- boolean b = _f.mkdirs();
- }
- String fName = Environment.getExternalStorageDirectory()
- + fileName;
- File f = new File(fName);
- if (!f.exists()) {
- if (f.getParentFile() == null) {
- f.getParentFile().mkdirs();
- }
- boolean b = f.createNewFile();
- }
- outStream = new FileOutputStream(f);
- outStream.flush();
- outStream.write(params[0]);
- outStream.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- Log.e("I/O", e.getMessage());
- } finally {
- }
- return null;
- }
- @Override
- protected void onPostExecute(Void result) {
- msv.camera.startPreview();
- if (clickCount != 3) {
- new StatusDialog(context, clickCount).show();
- TakePhotoActivity tpa = ((TakePhotoActivity) context);
- tpa.tvHeader.setText(tpa.getResources().getString(
- R.string.txtCaptureCouter)
- + " " + (clickCount + 1) + " /3");
- }
- }
- };
- public void prepareCounter() {
- String fileName = Environment.getExternalStorageDirectory()
- + "/org_Vilniaus_Energija/Vilniaus_Energija_" + counterType
- + "_" + counterNumber + "_" + dateTaken + "_";
- File f1 = new File(fileName + 1 + ".jpg");
- File f2 = new File(fileName + 2 + ".jpg");
- File f3 = new File(fileName + 3 + ".jpg");
- if (f1.exists()) {
- hCounter.photo1 = getByteArrayFromFile(f1);
- hCounter.sPhoto1 = fileName + 1 + ".jpg";
- if (f2.exists()) {
- hCounter.photo2 = getByteArrayFromFile(f2);
- hCounter.sPhoto2 = fileName + 2 + ".jpg";
- if (f3.exists()) {
- hCounter.photo3 = getByteArrayFromFile(f3);
- hCounter.sPhoto3 = fileName + 3 + ".jpg";
- } else {
- hCounter.photo3 = null;
- hCounter.sPhoto3 = null;
- }
- } else {
- hCounter.photo2 = null;
- hCounter.photo3 = null;
- hCounter.sPhoto2 = null;
- hCounter.sPhoto3 = null;
- }
- } else {
- hCounter.photo1 = null;
- hCounter.photo2 = null;
- hCounter.photo3 = null;
- hCounter.sPhoto1 = null;
- hCounter.sPhoto2 = null;
- hCounter.sPhoto3 = null;
- }
- sendCounter(hCounter);
- }
- public void sendCounter(final EntHeat heat) {
- Log.e("Files", heat.sPhoto1 + "::::" + heat.sPhoto2 + "::::"
- + heat.sPhoto3);
- final String user = AppValues.getUsername();
- final String pass = AppValues.getPassword();
- new AsyncTask<Void, Void, Void>() {
- ProgressDialog pd;
- int responseCode;
- String response;
- @Override
- protected void onPreExecute() {
- String TITLE = TakePhotoActivity.this.getResources().getString(
- R.string.app_name);
- String MESSAGE = TakePhotoActivity.this.getResources()
- .getString(R.string.sending);
- pd = ProgressDialog.show(TakePhotoActivity.this, TITLE,
- MESSAGE, true, false);
- responseCode = 0;
- response = null;
- }
- @Override
- protected Void doInBackground(Void... params) {
- WebClient webClient = new WebClient(Constants.DALKIA_URL
- + "/SaveHeat");
- webClient.authorize(user, pass);
- Gson gson = new Gson();
- final String json = gson.toJson(heat);
- final String JSON = "{"list": [" + json + "]}";
- int SIZE = 1000;
- int L = JSON.length() / SIZE;
- if (JSON.length() % SIZE != 0) {
- L++;
- }
- for (int i = 0; i < L; i++) {
- if (i != L - 1) {
- Log.e("Heat JSON",
- JSON.substring(i * SIZE, i * SIZE + SIZE));
- } else {
- Log.e("Heat JSON", JSON.substring(i * SIZE));
- }
- }
- try {
- webClient.ExecutePost(JSON);
- responseCode = webClient.getResponseCode();
- response = webClient.getResponse();
- } catch (Exception e) {
- }
- return null;
- }
- @Override
- protected void onPostExecute(Void result) {
- pd.dismiss();
- Context ctx = TakePhotoActivity.this;
- new StatusDialog(ctx, "").show();
- boolean notSent = false;
- if (responseCode != 200 || !"1".equals(response)) {
- notSent = true;
- }
- HeatAdapter ha = new HeatAdapter();
- ha.open(false);
- ha.saveHeat(new EntHeat[] { heat }, notSent, true, false);
- ha.close();
- }
- }.execute();
- }
- public void onBackPressed() {
- setResult(0);
- finish();
- }
- public void exit() {
- setResult(20);
- finish();
- }
- public static byte[] getByteArrayFromFile(File f) {
- byte[] array = null;
- try {
- InputStream is = new FileInputStream(f);
- long length = f.length();
- if (length > Integer.MAX_VALUE) {
- Log.e("TakePhotoError", "File " + f.getName()
- + " is too large.");
- } else {
- array = new byte[(int) length];
- int offset = 0;
- int numRead = 0;
- while (offset < array.length
- && ((numRead = is.read(array, offset, array.length
- - offset)) >= 0)) {
- offset += numRead;
- }
- if (offset < array.length) {
- Log.e("TakePhotoError",
- "Could not read file: " + f.getName());
- }
- is.close();
- }
- } catch (Exception e) {
- Log.e("TakePhotoError", "Exception: " + e.getMessage());
- }
- return array;
- }
- }
Add Comment
Please, Sign In to add comment