Welcome back in this video, you're going to learn how to use the media scanner connection in order
to share an image.
And it's a really cool tool where you can, for example, draw something and then share it.
And well, you can see here that you can now select where you want to share this image.
It will be possible via Gmail or whatever tools you have installed.
So let's say you have WhatsApp installed or any other imaging tool, you could use it here as well.
You can even share it with Bluetooth if you feel like it.
OK, so that's pretty much what we want to implement.
So let's go ahead and create a function just for that.
And the cool thing is, you will be able to just copy this function and use it for other projects that
you want to use it for.
So private fund share image and you could call it share, file or whatever.
So we need to pass in the directory or the result.
Well, we called the result earlier, so we could call it result here as well, even though this is
more like the directory.
So the path where it's at or the image is it.
So we're going to use the media scanner connection here, and it provides a way for applications to
pass a newly created or downloaded media file to the media scanner service.
And the media scanner service will read meter data from the file and add the file to the media content
provider.
And then it uses the Media Scanner Connection Client, which provides an interface for the media scanner
service to return to your iPhone and you'll be scanned file to the client of the media scanner connection
class.
So basically, what we can do is we can just call the scan file method here when the connection is established
with the media scanner and we can pass in the context as well as the path where we want to get our image
and we'll get the result from and I'm going to say array of.
And in my case, it's just one entry we can pass in the mime types that I'm going to not pass any.
I'm going to say it's now and then we can go ahead and use it.
So what we will get here will be the path and your eye.
So the path as well as the you were right and we can then use those.
So what can we do with them?
Well, first of all, we can use a share content here, which will be an intent.
Which?
I'm going to use share and tent dance action for so this will be in ten thought action sent.
So there's this property for intense, which is called action, and you can say what kind of action
you want this intend to do.
And in my case, I'm going to use action scent, which uses the Android Intend Action Scent, which
basically allows us to send items, and it opens up this feature that you saw earlier when we tested
it right?
And you can add extra details to it by using the put extra, so extra details to the intent because
it needs to know which direction it should use.
So which pilot should you, so to speak?
OK, so I'm going to use intent that extra stream has the name, and as the value that I'm passing,
I'm going to pass the Uri.
So this you are.
I didn't get it from here.
OK, so now we are passing basically where the image is on our device, and we're putting that into
the intent because the intent needs to know what it is we are sharing, right?
So we're sharing the image.
And then we need to define what kind of type it is, so share intent, that type.
And here you have multiple options.
So I'm going to say this is going to be a PNG file, so image PNG.
So that's the mime type of.
So I'm I am type of the data being handled by this intent.
And now we can pass this activity or start this activity by passing our share intent.
But that by itself is not going to be as simple.
We need to also say, well, we're also going to create a chooser which will use this share intent with
a title.
And I'm going to pass share.
So now this will basically start this thing here.
OK.
All right, that's pretty much it.
So the activity action displays an activity chooser, allowing this user to pick what they wanted before
proceeding.
OK, so now that we have our shared image method, we just need to call it at the right spot.
And what would be a good spot?
Well, I would say what the thing is, we can do it directly in the uncreated once we click on the save
button.
But it would be or when we think, the save button.
But the thing is, we don't know yet at this point if we have an image or not.
So inside of this get bitmap from view, that's where we are checking if we have an image.
So inside, get bitmap from you, that's we are creating the image.
And here we are checking if we do have an image.
So we only want to call this method once we have sent out the notification that the fire was saved successfully.
So here would be a good spot to call our share image and also, of course, need to know where the image
is at and the images add result.
So let's pass the result.
Because that's our absolute path, we could have called the path as well accordingly.
But at this point, we should be able to share the image.
So let me check it.
And let me draw something.
Let me click on it, and you see the share feature pops up.
Now, of course, there's one big thing that we need to change before the application is fully done,
and that is we need to get rid of this background image that we have defined as the basis.
So I'm going to put that in tools.
But not in the actual.
So let's run it again and see what we're going to get and you see we get this white background.
So let me draw a beautiful little face here.
And there we are.
So let's save this and share it via Gmail.
And you see Gmail opens up and now we could share it, even though I haven't set up Gmail on this emulator.
All right, so that's basically it.
So now if you ever want to use this feature in your own applications, the only thing that you will
need to have is going to be the path where damage is going to be.
So here you need to pass in the path.
It will create the Uri from it and then it will.
Well, you just need to use this code.
Cool.
So see you in the next project.