Guest User

Untitled

a guest
Jul 12th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.56 KB | None | 0 0
  1. public class MakePhotoActivity extends Activity {
  2.  
  3. // LogCat tag
  4. private static final String TAG = MainActivity.class.getSimpleName();
  5.  
  6.  
  7. // Camera activity request codes
  8. private static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 100;
  9. private static final int CAMERA_CAPTURE_VIDEO_REQUEST_CODE = 200;
  10.  
  11. public static final int MEDIA_TYPE_IMAGE = 1;
  12. public static final int MEDIA_TYPE_VIDEO = 2;
  13.  
  14. private Uri fileUri; // file url to store image/video
  15.  
  16. private Button btnCapturePicture, btnRecordVideo;
  17.  
  18. @Override
  19. protected void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.activity_make_photo);
  22.  
  23. // Changing action bar background color
  24. // These two lines are not needed
  25. // getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor(getResources().getString(R.color.action_bar))));
  26.  
  27. btnCapturePicture = (Button) findViewById(R.id.btnCapturePicture);
  28. btnRecordVideo = (Button) findViewById(R.id.btnRecordVideo);
  29.  
  30. /**
  31. * Capture image button click event
  32. */
  33. btnCapturePicture.setOnClickListener(new View.OnClickListener() {
  34.  
  35. @Override
  36. public void onClick(View v) {
  37. // capture picture
  38. captureImage();
  39. }
  40. });
  41.  
  42. /**
  43. * Record video button click event
  44. */
  45. btnRecordVideo.setOnClickListener(new View.OnClickListener() {
  46.  
  47. @Override
  48. public void onClick(View v) {
  49. // record video
  50. recordVideo();
  51. }
  52. });
  53.  
  54. // Checking camera availability
  55. if (!isDeviceSupportCamera()) {
  56. Toast.makeText(getApplicationContext(),
  57. "Sorry! Your device doesn't support camera",
  58. Toast.LENGTH_LONG).show();
  59. // will close the app if the device does't have camera
  60. finish();
  61. }
  62. }
  63.  
  64. /**
  65. * Checking device has camera hardware or not
  66. * */
  67. private boolean isDeviceSupportCamera() {
  68. if (getApplicationContext().getPackageManager().hasSystemFeature(
  69. PackageManager.FEATURE_CAMERA)) {
  70. // this device has a camera
  71. return true;
  72. } else {
  73. // no camera on this device
  74. return false;
  75. }
  76. }
  77.  
  78. /**
  79. * Launching camera app to capture image
  80. */
  81. private void captureImage() {
  82. Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  83.  
  84. fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
  85. intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
  86.  
  87. // start the image capture Intent
  88. //intent.putExtra("android.intent.extras.CAMERA_FACING", 1);
  89. startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
  90.  
  91.  
  92. }
  93.  
  94. /**
  95. * Launching camera app to record video
  96. */
  97. private void recordVideo() {
  98. Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
  99.  
  100. fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);
  101.  
  102. // set video quality
  103. intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
  104.  
  105. intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file
  106. // name
  107.  
  108. // start the video capture Intent
  109. startActivityForResult(intent, CAMERA_CAPTURE_VIDEO_REQUEST_CODE);
  110. }
  111.  
  112. /**
  113. * Here we store the file url as it will be null after returning from camera
  114. * app
  115. */
  116. @Override
  117. protected void onSaveInstanceState(Bundle outState) {
  118. super.onSaveInstanceState(outState);
  119.  
  120. // save file url in bundle as it will be null on screen orientation
  121. // changes
  122. outState.putParcelable("file_uri", fileUri);
  123. }
  124.  
  125. @Override
  126. protected void onRestoreInstanceState(Bundle savedInstanceState) {
  127. super.onRestoreInstanceState(savedInstanceState);
  128.  
  129. // get the file url
  130. fileUri = savedInstanceState.getParcelable("file_uri");
  131. }
  132.  
  133.  
  134.  
  135. /**
  136. * Receiving activity result method will be called after closing the camera
  137. * */
  138. @Override
  139. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  140. // if the result is capturing Image
  141. if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
  142. if (resultCode == RESULT_OK) {
  143.  
  144. // successfully captured the image
  145. // launching upload activity
  146. launchUploadActivity(true);
  147.  
  148.  
  149. } else if (resultCode == RESULT_CANCELED) {
  150.  
  151. // user cancelled Image capture
  152. Toast.makeText(getApplicationContext(),
  153. "User cancelled image capture", Toast.LENGTH_SHORT)
  154. .show();
  155.  
  156. } else {
  157. // failed to capture image
  158. Toast.makeText(getApplicationContext(),
  159. "Sorry! Failed to capture image", Toast.LENGTH_SHORT)
  160. .show();
  161. }
  162.  
  163. } else if (requestCode == CAMERA_CAPTURE_VIDEO_REQUEST_CODE) {
  164. if (resultCode == RESULT_OK) {
  165.  
  166. // video successfully recorded
  167. // launching upload activity
  168. launchUploadActivity(false);
  169.  
  170. } else if (resultCode == RESULT_CANCELED) {
  171.  
  172. // user cancelled recording
  173. Toast.makeText(getApplicationContext(),
  174. "User cancelled video recording", Toast.LENGTH_SHORT)
  175. .show();
  176.  
  177. } else {
  178. // failed to record video
  179. Toast.makeText(getApplicationContext(),
  180. "Sorry! Failed to record video", Toast.LENGTH_SHORT)
  181. .show();
  182. }
  183. }
  184. }
  185.  
  186. private void launchUploadActivity(boolean isImage){
  187. /* Intent i = new Intent(MakePhotoActivity.this, UploadActivity.class);
  188. i.putExtra("filePath", fileUri.getPath());
  189. i.putExtra("isImage", isImage);
  190. startActivity(i);*/
  191. }
  192.  
  193. /**
  194. * ------------ Helper Methods ----------------------
  195. * */
  196.  
  197. /**
  198. * Creating file uri to store image/video
  199. */
  200. public Uri getOutputMediaFileUri(int type) {
  201.  
  202. return Uri.fromFile(getOutputMediaFile(type));
  203. }
  204.  
  205.  
  206. /**
  207. * returning image / video
  208. */
  209. private static File getOutputMediaFile(int type) {
  210.  
  211. // External sdcard location
  212. File mediaStorageDir = new File(
  213. Environment
  214. .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
  215. Config.IMAGE_DIRECTORY_NAME);
  216.  
  217. // Create the storage directory if it does not exist
  218. if (!mediaStorageDir.exists()) {
  219. if (!mediaStorageDir.mkdirs()) {
  220. Log.d(TAG, "Oops! Failed create " + Config.IMAGE_DIRECTORY_NAME + " directory");
  221. return null;
  222. }
  223. }
  224.  
  225. // Create a media file name
  226. String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
  227. Locale.getDefault()).format(new Date());
  228. File mediaFile;
  229. if (type == MEDIA_TYPE_IMAGE) {
  230. mediaFile = new File(mediaStorageDir.getPath() + File.separator
  231. + "IMG_" + timeStamp + ".jpg");
  232. } else {
  233.  
  234. return null;
  235. }
  236.  
  237. return mediaFile;
  238. }
  239. }
Add Comment
Please, Sign In to add comment