Guest User

Untitled

a guest
Jan 12th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.65 KB | None | 0 0
  1. Android camera activity take picture freezes
  2. package org.my.activities;
  3.  
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileNotFoundException;
  7. import java.io.FileOutputStream;
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10.  
  11. import org.mokus.vilniaus.energija.R;
  12. import org.mokus.vilniaus.energija.database.HeatAdapter;
  13. import org.mokus.vilniaus.energija.dialogs.SDCardDialog;
  14. import org.mokus.vilniaus.energija.dialogs.StatusDialog;
  15. import org.mokus.vilniaus.energija.entities.EntHeat;
  16. import org.mokus.vilniaus.energija.helper.AppValues;
  17. import org.mokus.vilniaus.energija.helper.Constants;
  18. import org.mokus.vilniaus.energija.helper.Extras;
  19. import org.mokus.vilniaus.energija.helper.WebClient;
  20. import org.mokus.vilniaus.energija.layout.MeterAppSurfaceView;
  21.  
  22. import android.app.Activity;
  23. import android.app.ProgressDialog;
  24. import android.content.Context;
  25. import android.content.Intent;
  26. import android.hardware.Camera;
  27. import android.hardware.Camera.AutoFocusCallback;
  28. import android.hardware.Camera.Parameters;
  29. import android.media.AudioManager;
  30. import android.media.MediaPlayer;
  31. import android.net.Uri;
  32. import android.os.AsyncTask;
  33. import android.os.Bundle;
  34. import android.os.Environment;
  35. import android.util.Log;
  36. import android.view.View;
  37. import android.view.Window;
  38. import android.widget.Button;
  39. import android.widget.FrameLayout;
  40. import android.widget.RelativeLayout;
  41. import android.widget.TextView;
  42.  
  43. import com.google.gson.Gson;
  44.  
  45. public class TakePhotoActivity extends Activity {
  46. public Activity a;
  47.  
  48. public Button btnSave;
  49. public Parameters cameraParameters;
  50.  
  51. public EntHeat hCounter;
  52.  
  53. public String counterType;
  54. public String counterNumber;
  55. public String dateTaken;
  56.  
  57. public MeterAppSurfaceView svPhoto;
  58.  
  59. public Button btnSkip;
  60.  
  61. public static int clickCount;
  62. public static final int PHOTO_COUNT = 3;
  63.  
  64. public TextView tvHeader;
  65.  
  66. public MyPictureCallBack mpcb;
  67.  
  68. @Override
  69. protected void onCreate(Bundle b) {
  70. super.onCreate(b);
  71. requestWindowFeature(Window.FEATURE_NO_TITLE);
  72. setContentView(R.layout.camera_dialog);
  73.  
  74. Intent intent = getIntent();
  75. hCounter = null;
  76.  
  77. clickCount = 0;
  78.  
  79. try {
  80. hCounter = (EntHeat) intent.getSerializableExtra(Extras.COUNTER);
  81. } catch (Exception e) {
  82. Log.e("TakePhoto.Exception.79", e.getMessage());
  83. }
  84.  
  85. counterType = "siluma_mwh";
  86. counterNumber = "" + hCounter.id;
  87.  
  88. dateTaken = hCounter.dtaken;
  89. dateTaken = dateTaken.replace(":", "");
  90. dateTaken = dateTaken.replace("-", "");
  91. dateTaken = dateTaken.replace(" ", "");
  92.  
  93. initControls();
  94.  
  95. final File sdCard = Environment.getExternalStorageDirectory();
  96. if (!sdCard.exists()) {
  97. new SDCardDialog(this).show();
  98. }
  99.  
  100. btnSave.setOnClickListener(new View.OnClickListener() {
  101. public void onClick(View v) {
  102. if (!sdCard.exists()) {
  103. new SDCardDialog(TakePhotoActivity.this).show();
  104. return;
  105. }
  106. clickCount++;
  107.  
  108. btnSkip.setVisibility(View.VISIBLE);
  109.  
  110. new AsyncTask<Void, Void, Void>() {
  111. ProgressDialog pd;
  112.  
  113. @Override
  114. protected void onPreExecute() {
  115. String title = TakePhotoActivity.this.getResources()
  116. .getString(R.string.app_name);
  117. String pleaseWait = TakePhotoActivity.this
  118. .getResources().getString(R.string.please_wait);
  119. pd = ProgressDialog.show(TakePhotoActivity.this, title,
  120. pleaseWait, true, false);
  121. shootSound();
  122. }
  123.  
  124. @Override
  125. protected Void doInBackground(Void... params) {
  126. svPhoto.camera.stopPreview();
  127.  
  128. Camera c = svPhoto.camera;
  129. if (c != null) {
  130. c.autoFocus(new AutoFocusCallback() {
  131. public void onAutoFocus(boolean arg0,
  132. Camera camera) {
  133. svPhoto.camera.takePicture(null, null,
  134. null, mpcb);
  135. }
  136. });
  137. } else {
  138. }
  139.  
  140. try {
  141. new Thread().sleep(2000);
  142. } catch (InterruptedException e) {
  143. }
  144.  
  145. return null;
  146. }
  147.  
  148. @Override
  149. protected void onPostExecute(Void result) {
  150. Log.e("post", "execute");
  151. pd.dismiss();
  152.  
  153. if (clickCount == PHOTO_COUNT) {
  154. prepareCounter();
  155. }
  156. }
  157. }.execute();
  158. }
  159. });
  160.  
  161. btnSkip.setOnClickListener(new View.OnClickListener() {
  162. public void onClick(View v) {
  163. try {
  164. svPhoto.camera.stopPreview();
  165. svPhoto.camera.setPreviewCallback(null);
  166. svPhoto.camera.setPreviewDisplay(null);
  167. svPhoto.camera.setPreviewCallbackWithBuffer(null);
  168. svPhoto.camera.lock();
  169. svPhoto.camera.release();
  170. svPhoto = null;
  171. } catch (Exception e) {
  172. e.printStackTrace();
  173. }
  174. prepareCounter();
  175. }
  176. });
  177.  
  178. RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl1);
  179. rl.bringToFront();
  180. FrameLayout llPrev = (FrameLayout) findViewById(R.id.llPhotoPreview);
  181. llPrev.bringToFront();
  182. btnSave.bringToFront();
  183.  
  184. if (hCounter.status == 0) {
  185. btnSkip.setVisibility(View.INVISIBLE);
  186. }
  187.  
  188. mpcb = new MyPictureCallBack(this, svPhoto, counterType, counterNumber,
  189. dateTaken);
  190. }
  191.  
  192. public void onPause() {
  193. try {
  194. svPhoto.camera.stopPreview();
  195. svPhoto.camera.setPreviewCallback(null);
  196. svPhoto.camera.setPreviewDisplay(null);
  197. svPhoto.camera.setPreviewCallbackWithBuffer(null);
  198. svPhoto.camera.lock();
  199. svPhoto.camera.release();
  200. svPhoto = null;
  201. } catch (Exception e) {
  202. e.printStackTrace();
  203. }
  204. super.onPause();
  205. }
  206.  
  207. public void onResume() {
  208. super.onResume();
  209. if (svPhoto == null) {
  210. svPhoto = new MeterAppSurfaceView(this);
  211. svPhoto.setCounterTypeAndNumber(counterType, counterNumber,
  212. dateTaken);
  213. FrameLayout llPrev = (FrameLayout) findViewById(R.id.llPhotoPreview);
  214. llPrev.removeAllViews();
  215. llPrev.addView(svPhoto);
  216. }
  217. }
  218.  
  219. public void onDestroy() {
  220. onPause();
  221. super.onDestroy();
  222. }
  223.  
  224. public void shootSound() {
  225. AudioManager meng = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
  226. int volume = meng.getStreamVolume(AudioManager.STREAM_NOTIFICATION);
  227. MediaPlayer _shootMP = null;
  228.  
  229. if (volume != 0) {
  230. if (_shootMP == null)
  231. _shootMP = MediaPlayer
  232. .create(this,
  233. Uri.parse("file:///system/media/audio/ui/camera_click.ogg"));
  234. if (_shootMP != null)
  235. _shootMP.start();
  236. }
  237. }
  238.  
  239. public void initControls() {
  240. btnSave = (Button) findViewById(R.id.btnCapturePhoto);
  241. svPhoto = new MeterAppSurfaceView(this);
  242. svPhoto.setCounterTypeAndNumber(counterType, counterNumber, dateTaken);
  243. FrameLayout llPrev = (FrameLayout) findViewById(R.id.llPhotoPreview);
  244. llPrev.removeAllViews();
  245. llPrev.addView(svPhoto);
  246.  
  247. btnSkip = (Button) findViewById(R.id.imgSkip);
  248.  
  249. tvHeader = (TextView) findViewById(R.id.textView1);
  250. tvHeader.setText(getResources().getString(R.string.txtCaptureCouter)
  251. + " 1/3");
  252. }
  253.  
  254. public static class MyPictureCallBack implements Camera.PictureCallback {
  255. public String counterType, counterNumber, dateTaken;
  256. public MeterAppSurfaceView msv;
  257. public Context context;
  258.  
  259. public MyPictureCallBack(Context context, MeterAppSurfaceView msv,
  260. String counterType, String counterNumber, String dateTaken) {
  261. this.counterType = counterType;
  262. this.counterNumber = counterNumber;
  263. this.dateTaken = dateTaken;
  264. this.msv = msv;
  265. this.context = context;
  266. }
  267.  
  268. public void onPictureTaken(final byte[] data, final Camera camera) {
  269. new PicSaver(context, msv, counterType, counterNumber, dateTaken)
  270. .execute(data);
  271. }
  272. }
  273.  
  274. public static class PicSaver extends AsyncTask<byte[], Void, Void> {
  275. public String counterType, counterNumber, dateTaken;
  276. public MeterAppSurfaceView msv;
  277. public Context context;
  278.  
  279. public PicSaver(Context context, MeterAppSurfaceView msv, String cType,
  280. String cNumber, String dTaken) {
  281. Log.e("PicSaver", "create");
  282.  
  283. counterType = cType;
  284. counterNumber = cNumber;
  285. dateTaken = dTaken;
  286. this.msv = msv;
  287. this.context = context;
  288. }
  289.  
  290. @Override
  291. protected Void doInBackground(byte[]... params) {
  292. Log.e("savePhoto", "doInBackground");
  293.  
  294. String fileName = "/org_Vilniaus_Energija/Vilniaus_Energija_"
  295. + counterType + "_" + counterNumber + "_" + dateTaken + "_"
  296. + clickCount + ".jpg";
  297.  
  298. FileOutputStream outStream = null;
  299. try {
  300. File _f = new File(Environment.getExternalStorageDirectory()
  301. .toString() + "/org_Vilniaus_Energija");
  302. if (!_f.exists()) {
  303. boolean b = _f.mkdirs();
  304. }
  305.  
  306. String fName = Environment.getExternalStorageDirectory()
  307. + fileName;
  308. File f = new File(fName);
  309. if (!f.exists()) {
  310. if (f.getParentFile() == null) {
  311. f.getParentFile().mkdirs();
  312. }
  313. boolean b = f.createNewFile();
  314. }
  315.  
  316. outStream = new FileOutputStream(f);
  317. outStream.flush();
  318. outStream.write(params[0]);
  319. outStream.close();
  320. } catch (FileNotFoundException e) {
  321. e.printStackTrace();
  322. } catch (IOException e) {
  323. Log.e("I/O", e.getMessage());
  324. } finally {
  325. }
  326. return null;
  327. }
  328.  
  329. @Override
  330. protected void onPostExecute(Void result) {
  331. msv.camera.startPreview();
  332.  
  333. if (clickCount != 3) {
  334. new StatusDialog(context, clickCount).show();
  335. TakePhotoActivity tpa = ((TakePhotoActivity) context);
  336. tpa.tvHeader.setText(tpa.getResources().getString(
  337. R.string.txtCaptureCouter)
  338. + " " + (clickCount + 1) + " /3");
  339. }
  340. }
  341. };
  342.  
  343. public void prepareCounter() {
  344. String fileName = Environment.getExternalStorageDirectory()
  345. + "/org_Vilniaus_Energija/Vilniaus_Energija_" + counterType
  346. + "_" + counterNumber + "_" + dateTaken + "_";
  347.  
  348. File f1 = new File(fileName + 1 + ".jpg");
  349. File f2 = new File(fileName + 2 + ".jpg");
  350. File f3 = new File(fileName + 3 + ".jpg");
  351.  
  352. if (f1.exists()) {
  353. hCounter.photo1 = getByteArrayFromFile(f1);
  354. hCounter.sPhoto1 = fileName + 1 + ".jpg";
  355. if (f2.exists()) {
  356. hCounter.photo2 = getByteArrayFromFile(f2);
  357. hCounter.sPhoto2 = fileName + 2 + ".jpg";
  358. if (f3.exists()) {
  359. hCounter.photo3 = getByteArrayFromFile(f3);
  360. hCounter.sPhoto3 = fileName + 3 + ".jpg";
  361. } else {
  362. hCounter.photo3 = null;
  363. hCounter.sPhoto3 = null;
  364. }
  365. } else {
  366. hCounter.photo2 = null;
  367. hCounter.photo3 = null;
  368. hCounter.sPhoto2 = null;
  369. hCounter.sPhoto3 = null;
  370. }
  371. } else {
  372. hCounter.photo1 = null;
  373. hCounter.photo2 = null;
  374. hCounter.photo3 = null;
  375. hCounter.sPhoto1 = null;
  376. hCounter.sPhoto2 = null;
  377. hCounter.sPhoto3 = null;
  378. }
  379.  
  380. sendCounter(hCounter);
  381. }
  382.  
  383. public void sendCounter(final EntHeat heat) {
  384. Log.e("Files", heat.sPhoto1 + "::::" + heat.sPhoto2 + "::::"
  385. + heat.sPhoto3);
  386.  
  387. final String user = AppValues.getUsername();
  388. final String pass = AppValues.getPassword();
  389.  
  390. new AsyncTask<Void, Void, Void>() {
  391. ProgressDialog pd;
  392. int responseCode;
  393. String response;
  394.  
  395. @Override
  396. protected void onPreExecute() {
  397. String TITLE = TakePhotoActivity.this.getResources().getString(
  398. R.string.app_name);
  399. String MESSAGE = TakePhotoActivity.this.getResources()
  400. .getString(R.string.sending);
  401. pd = ProgressDialog.show(TakePhotoActivity.this, TITLE,
  402. MESSAGE, true, false);
  403. responseCode = 0;
  404. response = null;
  405. }
  406.  
  407. @Override
  408. protected Void doInBackground(Void... params) {
  409. WebClient webClient = new WebClient(Constants.DALKIA_URL
  410. + "/SaveHeat");
  411. webClient.authorize(user, pass);
  412.  
  413. Gson gson = new Gson();
  414. final String json = gson.toJson(heat);
  415.  
  416. final String JSON = "{"list": [" + json + "]}";
  417. int SIZE = 1000;
  418. int L = JSON.length() / SIZE;
  419. if (JSON.length() % SIZE != 0) {
  420. L++;
  421. }
  422. for (int i = 0; i < L; i++) {
  423. if (i != L - 1) {
  424. Log.e("Heat JSON",
  425. JSON.substring(i * SIZE, i * SIZE + SIZE));
  426. } else {
  427. Log.e("Heat JSON", JSON.substring(i * SIZE));
  428. }
  429. }
  430.  
  431. try {
  432. webClient.ExecutePost(JSON);
  433. responseCode = webClient.getResponseCode();
  434. response = webClient.getResponse();
  435.  
  436. } catch (Exception e) {
  437. }
  438.  
  439. return null;
  440. }
  441.  
  442. @Override
  443. protected void onPostExecute(Void result) {
  444. pd.dismiss();
  445. Context ctx = TakePhotoActivity.this;
  446. new StatusDialog(ctx, "").show();
  447.  
  448. boolean notSent = false;
  449. if (responseCode != 200 || !"1".equals(response)) {
  450. notSent = true;
  451. }
  452.  
  453. HeatAdapter ha = new HeatAdapter();
  454. ha.open(false);
  455. ha.saveHeat(new EntHeat[] { heat }, notSent, true, false);
  456. ha.close();
  457. }
  458. }.execute();
  459. }
  460.  
  461. public void onBackPressed() {
  462. setResult(0);
  463. finish();
  464. }
  465.  
  466. public void exit() {
  467. setResult(20);
  468. finish();
  469. }
  470.  
  471. public static byte[] getByteArrayFromFile(File f) {
  472. byte[] array = null;
  473.  
  474. try {
  475. InputStream is = new FileInputStream(f);
  476. long length = f.length();
  477.  
  478. if (length > Integer.MAX_VALUE) {
  479. Log.e("TakePhotoError", "File " + f.getName()
  480. + " is too large.");
  481. } else {
  482. array = new byte[(int) length];
  483.  
  484. int offset = 0;
  485. int numRead = 0;
  486. while (offset < array.length
  487. && ((numRead = is.read(array, offset, array.length
  488. - offset)) >= 0)) {
  489. offset += numRead;
  490. }
  491.  
  492. if (offset < array.length) {
  493. Log.e("TakePhotoError",
  494. "Could not read file: " + f.getName());
  495. }
  496.  
  497. is.close();
  498. }
  499. } catch (Exception e) {
  500. Log.e("TakePhotoError", "Exception: " + e.getMessage());
  501. }
  502.  
  503. return array;
  504. }
  505. }
Add Comment
Please, Sign In to add comment