How can I get notification of any file being deleted from SD card
Asked Answered
S

1

9

I want to create Dumpster like app, for this I want notification when user is deleting any file so that I can save it to my app memory.

I used File Observer but it is giving notification after file deletion and in marshmallow it does not notify for deletion also. I referred this link for file observer. Somewhere I read it is possible using native programming language (C), but couldn't get any solution. How can I do this? Thanks in advance.

I have tried this:

@Override
        public void onEvent(int event, String path) {
            if (path == null) {
                return;
            }
            //the monitored file or directory was deleted, monitoring effectively stops
            if ((FileObserver.DELETE_SELF & event)!=0) {
                FileAccessLogStatic.accessLogMsg += absolutePath + "/" + " is deleted\n";
            }       
        }
Sansom answered 23/6, 2017 at 11:45 Comment(7)
Show us what you have tried already. We're not here to write your code for you. Please read this: How do I ask a good question?Goosander
@Goosander I have updated my question.Sansom
Much better. This isn't my area and I can't answer your question, but at least now you have a chance of someone else answering :)Goosander
May be this can help. https://mcmap.net/q/1317208/-broadcast-receiver-or-sync-adapter-for-detection-of-deletion-of-files-at-particular-location-androidMarchpane
@AshishJohn I already have tried this, but it is giving notification after file deletion, read my question I have mentioned this.Sansom
https://mcmap.net/q/1317209/-how-can-i-notify-my-application-that-one-file-was-deleted-from-the-sdcard-android this link may help you.Drye
@DivySoni You can upvote and mark it as correct if you like. Thanks once again.Silkstocking
S
8

Lets clarify the things First.

  1. Dumpster Uses .trash directory which may and may not be present always. This should be noted that Dumpster does not run correctly in many devices as it can be seen through google reviews.
  2. Dumpster uses (i guessed it from the code for educational purpose only) it's own System File Handler that uses a service to check for the onClick event and if its a file onClick it saves the File as well as its path to a separate folder (usually hidden) and also saves it in a database that is local. If it is deleted you know where the file is and if not lets delete that file from that hidden folder. Well that's kinda not worth the pain because you need to almost make your service run for almost all the time which uses CPU resources. It also runs on rooted devices but why root device for this purpose only.
  3. As the Security in devices are increasing it is becoming less possible to perform these tasks. As latest of 1-09-2017 all these of files recycle bin have Negative reviews on latest android versions. Hence, proving my point.

    FileObserver uses the concept for checking any changes on the file or even directories but you cannot influence it meaning you cannot prevent deletion it will notify everything after the user has deleted.

    inotify.h it is the used for NDK purposes for creating application using to check the events on folders and files but if the folder is mentioned the sub sub folder will not be covered in this or notify you any change for the file. Moreover the concept used in inotify is same as FileObserver. you can only receive the notification after the file is deleted. The code used in the inotify is something like this.

  1. Create the inotify instance by inotify_init().
  2. Add all the directories to be monitored to the inotify list using inotify_add_watch() function.
  3. To determine the events occurred, do the read() on the inotify instance. This read will get blocked till the change event occurs. It is recommended to perform selective read on this inotify instance using select() call.
  4. Read returns list of events occurred on the monitored directories. Based on the return value of read(), we will know exactly what kind of changes occurred.
  5. In case of removing the watch on directories / files, call inotify_rm_watch().

The two methods present in inotify is as follow:

IN_DELETE – File/directory deleted from watched directory

IN_DELETE_SELF – Watched file/directory was itself deleted

both of which are almost same as in FileObserver

  1. This Solution can be of help not fully but still can help in creating any Dumpster type Application. It can be said that you need to create your own File Manager where you can Create your own Custom FileV2(Just a cool name File version 2.0) class that extends File and you can override the delete method (and all others) as you like. You can create a custom pop up saying do you want to delete the file with your own backing up the file on yes and dismissing the pop up on no. (Make Sure User uses this File Manager to Delete otherwise it will not work because overriding the system File delete() will just mess up other applications as well).

    class filev2 extends File {
    
    public filev2(@NonNull String pathname) {
        super(pathname);
    }
    
    public filev2(@NonNull URI uri) {
        super(uri);
    }
    
    @Override
    public boolean delete() {
     //   return super.delete();
    //Do as you want and return the boolean.
    }
    
    }
    

But make sure your files will be saved if the user uses your File Manager for this. You can set the intent-filters for the task so that your FileManager comes in ACTION_VIEW for that matter.

Last but I am not sure about this maybe registerContentObserver can be used also. (Not Sure though)

Sources:

Inotify.h help website

registerContentObserver help

Kind of Similar Question

FileObserver Help

Linux Help Deleted Log for Files

I hope it helps and I hope you can now have a start to what you want.

Silkstocking answered 1/9, 2017 at 15:26 Comment(2)
Your answer is really appreciated, that's why I vote up your answer and given you bounty but it couldn't solve my problem. .trash folder is used by other applications like this, that's why other applications doesn't provide efficient result like dumpster, from my research I get It is using some Native code (C language) for this.Sansom
even the dumpster doesnot provide me efficient result on my mobile phone. I checked its code for educational purpose. They are kinda using a service to check and for rooted devices they can do anything super user type. With new android coming it is difficult to create what you want. Dumpster doesnot run on Oreo in case of my mobile even after giving specified permissions. Talking about Native C you need to code and override many basic methods for this and it is kind of not difficult but not worth the pain.Silkstocking

© 2022 - 2024 — McMap. All rights reserved.