Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
In this video we will learn how to save pictures and videos to the gallery and keep them even if our app is deleted.
Code for Copy/Paste:
String fileName = "";
String fileType = "";
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
if (mediaType == MEDIA_TYPE_IMAGE) {
fileName = "IMG_"+ timeStamp;
fileType = ".jpg";
} else if(mediaType == MEDIA_TYPE_VIDEO) {
fileName = "VID_"+ timeStamp;
fileType = ".mp4";
} else {
return null;
}
Project Files
-
0:00
In an app that creates something, pictures, video, sounds or
-
0:03
whatever, we need to decide where to store the output.
-
0:06
Do we want it to live on if our app is deleted or
-
0:09
should content created by our app be deleted as well?
-
0:12
In this case, we're going to save our pictures and
-
0:14
videos to the photos up and let it stay even if our app is deleted.
-
0:18
Users can delete pictures from the photos app manually if they want.
-
0:23
All right, so we are set in the takePhoto method.
-
0:25
Let's go back down to our getOutput method,
-
0:28
and here before we return anything, let's add a few steps in comments so
-
0:33
we know what we're doing, then we'll fill in each piece.
-
0:36
So first we want to get the external storage directory.
-
0:42
Then we want to create a unique file name.
-
0:47
Thirdly, we will create the file.
-
0:51
And then finally, we will return the file's URI.
-
0:56
For step one the method we want returns a file object.
-
0:59
So let's create a file variable named mediaStorageDir for directory.
-
1:05
The file object can be either a file or directory.
-
1:09
We will set this with a special method called getExternalFilesDir.
-
1:15
This method requires another environment constant as a parameter.
-
1:18
So let's type Environment.
-
1:21
And then look at the list here.
-
1:22
There are a few different options here and I'd recommend checking out the environment
-
1:25
class documentation to understand when they're useful.
-
1:28
We want to use directory underscore pictures.
-
1:32
Because it will allow our photos and
-
1:33
videos to be stored in a place that will exist even if our app is deleted.
-
1:38
Now before we go on I just want to point out one thing.
-
1:40
Getting the external files like this works on newer releases of a Android.
-
1:45
But depending on your device and the version of Android it's running
-
1:47
you may need to do it a little bit differently.
-
1:49
I'll put some additional information in the teacher's notes, but
-
1:52
just know that if you're having trouble with this you might need to investigate
-
1:55
how you're getting the external files directory.
-
1:58
So next we need a unique file name for the file that will hold the image or
-
2:01
video in return its URI.
-
2:04
If we use the same name then the file would get overwritten with each
-
2:07
photo or video.
-
2:08
So a common practice is to append a timestamp in the name itself.
-
2:11
In the interest of time I'm going to paste this code in but it's available for
-
2:15
you to copy and paste in the teacher's notes.
-
2:17
All right, so let's check out what I just pasted in I'm going to move this over
-
2:20
a little bit and center the code.
-
2:22
And the first thing I have is an error because I can't resolve
-
2:25
the day classify it.
-
2:25
If I hit all plus enter, I wanna import the class and
-
2:28
I want the java.util version not the java.sql version.
-
2:31
So what we're doing here is we're creating a file name and adding a file type.
-
2:36
The unique timestamp is formatted with this simple date formatter.
-
2:40
And then based on the type of request are doing if it's an image we're going to
-
2:43
prefix it with img append the timeStamp and then use the .JPEG file extension.
-
2:49
Otherwise, if it's a video then we will prefix with vid,
-
2:53
do the timeStamp and then add the file type of mp4.
-
2:56
So next for step three,
-
2:59
we can create a temp version of the file where the photo will be stored.
-
3:03
We need this file to exist for the camera to work correctly.
-
3:06
So let's say, File mediaFile = File.createTempFile and
-
3:12
then get pass in the parts that we defined up above.
-
3:16
FileName, fileType and the directory mediaStorageDir.
-
3:21
Let's add a log statement here to take a look at the file path being created.
-
3:25
Log.i TAG File: then + Uri.fromFile and
-
3:31
we can pass in mediaFile.
-
3:36
Okay now we're getting an error here because this is throwing an exception so
-
3:39
we need to handle it.
-
3:39
So let's say try these lines and
-
3:43
then we'll catch an IOException named E.
-
3:47
And here all we will do is add a log statement Log.e TAG,
-
3:53
we’ll say error creating a file colon and
-
3:58
then we'll add to this mediaStorageDir.getAbsolutePath
-
4:04
plus the file name, plus the file type.
-
4:09
Once again I'm gonna drop this down just so it will all fit on the same screen.
-
4:14
Now we're finally ready to return the URI that we need.
-
4:18
We can us a URI class method to get the URI for a file object.
-
4:22
So we say instead of return null let's return
-
4:26
Uri.framefile mediaFile.
-
4:32
Oops and I got my scope wrong because I declared
-
4:36
media file inside of the try blocks so now I need to move it out here.
-
4:39
Let's say file mediaFile was declared up here at the same level of scope and
-
4:44
I can get rid of the file keyword.
-
4:46
That's right, we only want to return this inside of the try block, so
-
4:49
let me cut that from here.
-
4:52
Pasted up here and again if anything goes wrong we will catch the exception and
-
4:56
then down here at the bottom we will return null.
-
4:58
Okay, let's run this and see what happens.
-
5:03
Okay so our app has started.
-
5:04
Let's try capturing a photo and get that green box right there.
-
5:10
I'll hit the check mark.
-
5:11
And we come back to main activity and now if we look over here we can see in the log
-
5:16
that the actual file path for the photo, cool.
-
5:20
Look at this and all the way over to the right,
-
5:23
we see that image with the unique timeStamp that we created.
-
5:27
Now the cool thing is this is an actual file path on the device and
-
5:30
we can go look at it.
-
5:31
So let's check it out in the Android device monitor.
-
5:35
So it's going to ask about disabling ADB Integration that's okay because
-
5:38
it's going to stop its connection with our emulator.
-
5:41
Okay, so the device monitor has our emulator listed over here on the left and
-
5:45
I want to change to the file explorer tab.
-
5:47
And then I'm going to walk this same path storage emulated zero etc., etc.
-
5:53
Let's go here.
-
5:54
We’ll click on storage and emulated and
-
5:57
it looks like I'm gonna have to pull this over to see more, Zero > Android >
-
6:02
Data > com.teamtreehouse
-
6:08
> files > Pictures and finally, the actual image itself.
-
6:13
Now we can't see it from here, but we can save it to our local computer.
-
6:18
So I'm going to save this to my home drive.
-
6:22
And check it out, here in finder I can see the image file itself and
-
6:28
it's a great shot of that green box almost on the left side of the screen, cool.
-
6:33
All right, so let's close this.
-
6:36
I'm going to close the device monitor.
-
6:37
And in the next video,
-
6:39
we'll see how to view this saved photo inside of an image view.
You need to sign up for Treehouse in order to download course files.
Sign up