Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 5.1
- // To put in icons, copy and paste them into each of the drawable folders in Res folder.
- // 5.2 Add Dialog to choose to take a photo or to take a video
- https://teamtreehouse.com/library/build-a-selfdestructing-message-android-app/capturing-photos-and-videos/presenting-a-list-of-choices-in-a-dialog
- // 5.4 Intent launches one of your camera apps on your phone and you take a picture with it
- https://teamtreehouse.com/library/build-a-selfdestructing-message-android-app/capturing-photos-and-videos/taking-a-photo-using-an-intent
- // 5.5 Checking External Storage
- // Checks the External Storage/SD Card if mounted
- // 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
- //5.6 Setting where photos are saved
- if (mMedia Uri == null) {
- //previous lesson
- }
- else {
- //this lesson we do the 'else' part. Previous lesson we did the if External storage is 'null' part
- 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.
- //Also learned how to check Android Device Monitor and check File Explorer for where the saved pictures are
- // 5.7-5.9 Saving a Photo to Gallery
- // 5.9 We accept the photo from the Camera that is started with 'startActivityForResult()' method.
- // so we get a result back. 'so onActivityResult()' method will be called.
- // this is where we do something with the image
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- if(resultCode == RESULT_OK) {
- //add file to the Gallery using intent
- 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
- mediaScanIntent.setData(mMediaUri);
- sendBroadcast(mediaScanIntent);
- }
- else if (resultCode != RESULT_CANCELED) {
- Toast.makeText(this, R.string.general_error, Toast.LENGTH_LONG).show();
- }
- }
- //5.10 We can use the camera for videos pretty much the same way as we did for photos
- // We'll start it via an intent, but we'll configure the intent a little bit differently to specify a video.
- // We'll limit the video capture so we won't have hour long videos
- case 1: //index 1 = Take Video
- Intent videoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
- 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
- if(mMediaUri == null) { //copied from case 0
- Toast.makeText(MainActivity.this, R.string.error_external_storage , Toast.LENGTH_LONG).show();
- }
- else {
- videoIntent.putExtra(MediaStore.EXTRA_OUTPUT, mMediaUri);
- videoIntent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 10); //limit video to 10 seconds
- 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
- startActivityForResult(videoIntent, TAKE_VIDEO_REQUEST);
- }
- break;
- //5.12 Select a photo from a gallery to send to friend option instead of using captured photo
- case 2: //index 2 = Choose Picture //5.12
- Intent choosePhotoIntent = new Intent(Intent.ACTION_GET_CONTENT);
- choosePhotoIntent.setType("image/*"); //specify only images (not songs or videos)
- startActivityForResult(choosePhotoIntent, PICK_PHOTO_REQUEST);
- break;
- //after we pic a photo, the "result is returned" aka jump to OnActivityResult method below.
- //----code excerpted from activity---
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) { //[2] // 'intent' third parameter is named 'data' and that's where our URI will be.
- super.onActivityResult(requestCode, resultCode, data);
- if(resultCode == RESULT_OK) {
- //5.12 If request code equals one of our PICK codes, PICK_PHOTO, or PICK_VIDEO:
- if (requestCode == PICK_PHOTO_REQUEST || requestCode == PICK_VIDEO_REQUEST) {
- if(data == null) { //if data is null, present error toast
- Toast.makeText(this, getString(R.string.general_error), Toast.LENGTH_LONG).show();
- } else { //else get the data(the picture from the gallery)
- mMediaUri = data.getData();
- }
- } else {
- //add file to the Gallery using intent
- 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
- mediaScanIntent.setData(mMediaUri);
- sendBroadcast(mediaScanIntent); //sends the broadcast intent
- }
- }
- else if (resultCode != RESULT_CANCELED) {
- Toast.makeText(this, R.string.general_error, Toast.LENGTH_LONG).show();
- }
- }
- // 5.13 Selecting A Video From Gallery
- // 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.
- // And then in onActivityResult we check the file size the user selects using InputStream
- // input stream is used to stream information from the file byte by byte.
- // 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.
- //make sure to catch exceptions
- public static final int FILE_SIZE_LIMIT = 1024*1024*10; //[4] /5.13 part 2 convert bytes to megabytes
- //excerpt from @Override
- // protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- //5.13 If request code is a video request...
- if (requestCode == PICK_VIDEO_REQUEST) {
- // ...makes sure the file is less than 10 mb
- int fileSize = 0; //declare int variable named fileSize and initialize it to 0
- InputStream inputStream = null; //declare input stream so it can be used in both 'try { }' scope and 'finally { }' scope [**] //simple
- try {
- //open up an InputStream to the file // input stream is used to stream information from the file byte by byte.
- 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.
- //now with the inputS stream, we can get the total number of bytes avaliable by calling inputStream.available
- fileSize = inputStream.available();
- }
- // red squiggly line under InputStream and getContentResolve because need to catch possible exceptions //simple.
- catch (FileNotFoundException e) {
- Toast.makeText(this, R.string.error_opening_file, Toast.LENGTH_LONG).show();
- return; //error, return back to activity
- }
- catch (IOException e) {
- Toast.makeText(this, R.string.error_opening_file, Toast.LENGTH_LONG).show();
- return; //error, return back to activity
- }
- //In java we need to remember to close our input streams. Otherwise we will have memory leaks:
- //we close it with "finally block", it always gets executed no matter what
- finally {
- try {
- inputStream.close(); //[**]
- } catch (IOException e) {/*this is intentionally blank*/ }
- }
- // if file size is greater than or equal to 10mbs
- if (fileSize >= FILE_SIZE_LIMIT){ //[4]
- Toast.makeText(this, R.string.error_file_size_too_large, Toast.LENGTH_LONG).show();
- return; //since the file selected is too big, return back to the activity instead of continuing on
- }
- }
- //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