Advertisement
yo2man

Capturing Photos and Videos Treehouse Lesson

Sep 3rd, 2015
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.03 KB | None | 0 0
  1. // 5.1
  2. // To put in icons, copy and paste them into each of the drawable folders in Res folder.
  3.  
  4. // 5.2 Add Dialog to choose to take a photo or to take a video
  5. https://teamtreehouse.com/library/build-a-selfdestructing-message-android-app/capturing-photos-and-videos/presenting-a-list-of-choices-in-a-dialog
  6.  
  7. // 5.4 Intent launches one of your camera apps on your phone and you take a picture with it
  8. https://teamtreehouse.com/library/build-a-selfdestructing-message-android-app/capturing-photos-and-videos/taking-a-photo-using-an-intent
  9.  
  10. // 5.5 Checking External Storage
  11. // Checks the External Storage/SD Card if mounted
  12. // We are basically going to follow the guidelines of saving files from the Android Developer documentation: http://developer.android.com/guide/topics/media/camera.html#saving-media
  13.  
  14. //5.6 Setting where photos are saved
  15. if (mMedia Uri == null) {
  16. //previous lesson
  17. }
  18. else {
  19. //this lesson we do the 'else' part. Previous lesson we did the if External storage is 'null' part
  20.  
  21. There are two options in the documentation(http://developer.android.com/guide/topics/media/camera.html#saving-media), you choose so that the output(photos/videos) are deleted when the app is unistalled or have the output stay in the gallery even after the app is uninstalled.
  22. //Also learned how to check Android Device Monitor and check File Explorer for where the saved pictures are
  23.  
  24.  
  25. // 5.7-5.9 Saving a Photo to Gallery
  26.     // 5.9 We accept the photo from the Camera that is started with 'startActivityForResult()' method.
  27.     // so we get a result back. 'so onActivityResult()' method will be called.
  28.     // this is where we do something with the image
  29.     @Override
  30.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  31.         super.onActivityResult(requestCode, resultCode, data);
  32.  
  33.         if(resultCode == RESULT_OK) {
  34.             //add file to the Gallery using intent
  35.             Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); //broadcasting the intent to tell gallery that new files are available //the gallery/picture apps can listen for broadcasts
  36.             mediaScanIntent.setData(mMediaUri);
  37.             sendBroadcast(mediaScanIntent);
  38.         }
  39.         else if (resultCode != RESULT_CANCELED) {
  40.             Toast.makeText(this, R.string.general_error, Toast.LENGTH_LONG).show();
  41.         }
  42.     }
  43.  
  44.  
  45.  
  46. //5.10 We can use the camera for videos pretty much the same way as we did for photos
  47. // We'll start it via an intent, but we'll configure the intent a little bit differently to specify a video.
  48. // We'll limit the video capture so we won't have hour long videos
  49.  
  50.     case 1: //index 1 = Take Video
  51.                             Intent videoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
  52.                             mMediaUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO); //just like an image we need a URI to know where to store the file. Except this time we can use the same method as before
  53.                             if(mMediaUri == null) { //copied from case 0
  54.                                 Toast.makeText(MainActivity.this, R.string.error_external_storage , Toast.LENGTH_LONG).show();
  55.                             }
  56.                             else {
  57.                                 videoIntent.putExtra(MediaStore.EXTRA_OUTPUT, mMediaUri);
  58.                                 videoIntent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 10); //limit video to 10 seconds
  59.                                 videoIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0); //lower quality of video to lowest quality //only two qualities available in camera1 api. 0 for low quality. 1 for high quality
  60.                                 startActivityForResult(videoIntent, TAKE_VIDEO_REQUEST);
  61.                             }
  62.                             break;
  63.  
  64. //5.12 Select a photo from a gallery to send to friend option instead of using captured photo
  65.  
  66. case 2: //index 2 = Choose Picture //5.12
  67.                             Intent choosePhotoIntent = new Intent(Intent.ACTION_GET_CONTENT);
  68.                             choosePhotoIntent.setType("image/*"); //specify only images (not songs or videos)
  69.                             startActivityForResult(choosePhotoIntent, PICK_PHOTO_REQUEST);
  70.                             break;
  71.                         //after we pic a photo, the "result is returned" aka jump to OnActivityResult method below.
  72.  
  73.  
  74.  
  75. //----code excerpted from activity---
  76.  
  77. @Override
  78.     protected void onActivityResult(int requestCode, int resultCode, Intent data) { //[2] // 'intent' third parameter is named 'data' and that's where our URI will be.
  79.         super.onActivityResult(requestCode, resultCode, data);
  80.  
  81.         if(resultCode == RESULT_OK) {
  82.  
  83.         //5.12 If request code equals one of our PICK codes, PICK_PHOTO, or  PICK_VIDEO:
  84.         if (requestCode == PICK_PHOTO_REQUEST || requestCode == PICK_VIDEO_REQUEST) {
  85.             if(data == null) { //if data is null, present error toast
  86.                     Toast.makeText(this, getString(R.string.general_error), Toast.LENGTH_LONG).show();
  87.             }  else { //else get the data(the picture from the gallery)
  88.                 mMediaUri = data.getData();
  89.  
  90.             }
  91.         } else {
  92.                 //add file to the Gallery using intent
  93.                 Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); //create the broadcasting the intent to tell gallery that new files are available //the gallery/picture apps can listen for broadcasts
  94.                 mediaScanIntent.setData(mMediaUri);
  95.                 sendBroadcast(mediaScanIntent); //sends the broadcast intent
  96.             }
  97.         }
  98.         else if (resultCode != RESULT_CANCELED) {
  99.             Toast.makeText(this, R.string.general_error, Toast.LENGTH_LONG).show();
  100.         }
  101.     }
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108. // 5.13 Selecting A Video From Gallery
  109. // pretty much the same as selecting a photo. However unlike a photo, video file sizes can be too large. So we create a toast warning the user to not select file size over 10 MB.
  110. // And then in onActivityResult we check the file size the user selects using InputStream
  111. // input stream is used to stream information from the file byte by byte.
  112.  
  113. // and then convert bytes to megabytes. Pretty much a waste of time doing this though because there really isn't a need. Can just use bytes just fine.
  114.    
  115.         //make sure to catch exceptions
  116.  
  117.  
  118. public static final int FILE_SIZE_LIMIT = 1024*1024*10; //[4] /5.13 part 2 convert bytes to megabytes
  119.  
  120. //excerpt from @Override
  121. //    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  122.  
  123. //5.13 If request code is a video request...
  124.             if (requestCode == PICK_VIDEO_REQUEST) {
  125.                 // ...makes sure the file is less than 10 mb
  126.                 int fileSize = 0; //declare int variable named fileSize and initialize it to 0
  127.  
  128.                 InputStream inputStream = null; //declare input stream so it can be used in both 'try { }' scope and 'finally { }' scope [**] //simple
  129.                 try {
  130.                     //open up an InputStream to the file //  input stream is used to stream information from the file byte by byte.
  131.                      inputStream = getContentResolver().openInputStream(mMediaUri); //[**] //In this line, the get content resolver method resolves the content URI for us, to the actual file on the device.
  132.                     //now with the inputS stream, we can get the total number of bytes avaliable by calling inputStream.available
  133.                     fileSize = inputStream.available();
  134.                 }
  135.                 // red squiggly line under InputStream and getContentResolve because need to catch possible exceptions //simple.
  136.                 catch (FileNotFoundException e) {
  137.                     Toast.makeText(this, R.string.error_opening_file, Toast.LENGTH_LONG).show();
  138.                     return; //error, return back to activity
  139.                 }
  140.                 catch (IOException e) {
  141.                     Toast.makeText(this, R.string.error_opening_file, Toast.LENGTH_LONG).show();
  142.                     return; //error, return back to activity
  143.                 }
  144.                 //In java we need to remember to close our input streams. Otherwise we will have memory leaks:
  145.                 //we close it with "finally block", it always gets executed no matter what
  146.                 finally {
  147.                     try {
  148.                         inputStream.close(); //[**]
  149.                     } catch (IOException e) {/*this is intentionally blank*/ }
  150.                 }
  151.  
  152.                 // if file size is greater than or equal to 10mbs
  153.                 if (fileSize >= FILE_SIZE_LIMIT){ //[4]
  154.                     Toast.makeText(this, R.string.error_file_size_too_large, Toast.LENGTH_LONG).show();
  155.                     return; //since the file selected is too big, return back to the activity instead of continuing on
  156.                 }
  157.             }
  158.  
  159.  
  160.  
  161.  
  162. //If we want to use outside camera instead of the camera api we are using right now. we can just copy all this code and paste it in. Just the integers that link up the cases.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement