首页 > 解决方案 > 在图库中删除图像后如何触发广播接收器?

问题描述

我正在开发一个 android 应用程序,该应用程序应该具有从 android 库中删除的图像的副本。如果用户想从图库中删除图片,在他/她删除之前,我们的应用程序必须将图像的副本保存在隐藏文件夹中。我使用了 SMS 接收器,但这有点令人困惑。


       public MyReceiver() {
       }

       @Override
       public void onReceive(Context context, Intent intent) {
            //activity which I want to perform here is to have a copy of deleted image in a hidden folder
       }

标签: androidbroadcastreceiverandroid-broadcast

解决方案


当文件被删除时,您无法跟踪它。您可以使用文件观察器来跟踪文件夹中的其他内容。

observer = new FileObserver(Your_folder_Path) { // set up a file observer to watch this directory on sd card

 @Override
 public void onEvent(int event, String file) {
     //if(event == FileObserver.CREATE && !file.equals(".probe")){ // check if its a "create" and not equal to .probe because thats created every time camera is launched
     Log.d(TAG, "File created [" + pathToWatch + file + "]");

     Toast.makeText(getBaseContext(), file + " was saved!", Toast.LENGTH_LONG).show();
     //}
 }
};
 observer.startWatching();

推荐阅读