Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Farouk Charkas
1,957 PointsRecovery database
I would like to make an app where it detects when a user deletes a photo and stores stories in parse how would I do that ? I know the parse part but I need help on the Gallery Delete Recovery
Farouk Charkas
1,957 PointsOkay Carla Brooks, I was quite sad when I discovered the app Dumpster did basically the same thing. Anyhow, so, have you ever tried Dumpster, because that is what I need to make except with photos. The user deletes a photo in the default app named 'Gallery', the photo gets sent to the app. Then a user could go to the app and either delete it for good or just simply download it again.
1 Answer
Harry James
14,780 PointsHey Evering Applications!
To achieve an app similar to that of Dumpster, you will probably want to use Android's FileObserver class. Simply extend the class and implement the required method of onEvent(). In the constructor, you will need to specify a path. As you want to collect images from the Gallery, I recommend you use the line of code I have attached below for the path:
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
Once you've got the path set up, remember to startWatching()!
If an event is called, the onEvent() method will run. Here, check to make sure it is the DELETE event that is being called. If it is anything else, break out of the method (This method actually returns void, so to achieve this just only run code if the condition of it being a deletion event is met), we're not interested in it! When we know it is a deletion event, we can then run any code we want to (I would however, just break out if it's not a known image file so that we don't run into any issues. Note we can't call an exception here).
Finally, you will likely want to use a Service to avoid the issue of garbage collection (Android clears up unused activities to save on resources) - this is well worth a read!
Hopefully this information should help you to get this app working! If you run into any problems on the way though, we're here to help out :)
Farouk Charkas
1,957 PointsI appreciate your help Mr. James. On the first read I did not quite get it, could you post the full code so I can understand it better. Thank you so much for this, in order to keep it for other people, can you propose a name for this question?
And here is some questions: What is Environment? Is Android referring to Gallery by Directory? What is the startWatching() and the onEvent() methods
Farouk Charkas
1,957 PointsSo would I do something like this?
String recoveryPhotoPath;
recoveryPhotoPath =
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
Harry James
14,780 PointsHey Farouk!
Yes, exactly what you have written is correct for the path.
Typically, we only provide pointers in the Community just so that we can get around to more people but I'm still willing to provide you with some code for you to take a look at.
I've gone ahead and set up a FileObserver in an activity, it looks a bit like this:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Get the path where pictures are generally stored.
String recoveryPhotoPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
.toString();
// Let's just check where Android thought that was.
Log.d("MainActivity", "Path: " + recoveryPhotoPath);
// Let's create a new FileObserver. I've created it as an anonymous inner class here, you don't have to.
FileObserver observer = new FileObserver(recoveryPhotoPath) {
@Override
public void onEvent(int event, String path) {
// if the event is a DELETE event, run this code:
if (event == FileObserver.DELETE) {
Log.d("MainActivity", "FILE DELETED AT " + path);
showToast();
}
// else if it's not, we're just going to break out of here. Let's not waste resources processing code!
}
};
// Tell the observer to start watching for changes.
observer.startWatching();
}
// Toasts need to be shown outside of the anonymous inner class.
public void showToast() {
Toast.makeText(this, "It's working!", Toast.LENGTH_LONG).show();
}
Note that this is inside an activity and on most devices (This happened on mine when testing) you will find this code doesn't work as the activity gets destroyed when the app is closed. When the activity is destroyed, the observer then stops watching, so we're not notified of anything that happens. If you want to test it, you could delete a file by running code to do so inside your app.
This is where them Services that I mentioned earlier come into play - you can set one up so that the FileObserver remains watching, even when the app is closed. Go ahead and read this document to learn how to set one up! I would say a service is mandatory for making this type of app, so that it works on all devices.
As for your questions, Environment gives us access to environment variables. These are generally set by the phone manufacturer and allow us developers to say "Give me the pictures directory" to the Environment class and we'll then be given that directory for the specific device (Note that I have added a log statement just to check this, as it's not always correct so it may have to be adjusted for some devices). Hopefully this should also answer your second question in that we're using the Environment to get the directory where images are being stored.
As for the two methods, startWatching() is used to tell the FileObserver to start watching the directory we've specified for any changes. Nothing will happen if we don't call this method. onEvent() is called whenever there is an "event" relating to the files in the directory we're watching. This method is called whenever we try to access a file in the directory, create a new file, delete a file etc... The full list of events can be found on the Constants of FileObserver
Hopefully this should explain everything for you! As always, if you have any more questions, feel free to give me a shout :)
Carla Brooks
803 PointsCarla Brooks
803 PointsCan you explain a little bit the first part?... Maybe you should consider using a boolean and store it on SharedPreferences, if its true then the user deleted the photo, and set the default value as false