美文网首页Android
【Android】监听文件的创建

【Android】监听文件的创建

作者: 小八八八八八八 | 来源:发表于2019-07-11 17:50 被阅读0次

    需求是监听到指定目录创建文件后,将其移动到其他目录,并将原文件无效化。因为直接删除会在通知栏提示,不得已采用此方法
    这里我将华为截屏的图片移动到其他目录。
    先上效果图


    Gif.gif

    其中的MP4文件是录屏文件。

    监听文件,我用到了FileObserver

    public class SDCardListener extends FileObserver {
        private static final String TAG = "SDCardListener";
        private Context mContext;
        public static String mSrcPath = "/storage/emulated/0/Pictures/Screenshots/";
        private String mDecPath = "/storage/emulated/0/Pictures/Dec/";
    
        public SDCardListener(String path, Context context) {
            super(path);
            this.mContext = context;
        }
    
        @Override
        public void onEvent(int event, @Nullable String path) {
            switch (event) {
                case FileObserver.CLOSE_WRITE:
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            if (!path.endsWith("jpg")) {
                                return;
                            }
                            File srcFile = new File(mSrcPath, path);
                            File decFile = new File(mDecPath, path);
                            FileUtil.fileChannelCopy(srcFile.getAbsolutePath(), decFile .getAbsolutePath());
                            SystemClock.sleep(1000);
                            srcFile.renameTo(new File(mSrcPath, "temp"));
                            srcFile = new File(mSrcPath, "temp");
                            FileOutputStream fileOutputStream = null;
                            try {
                                fileOutputStream = new FileOutputStream(srcFile);
                                fileOutputStream.write("".getBytes());
                                fileOutputStream.flush();
                            } catch (Exception e) {
                                e.printStackTrace();
                                Log.d(TAG, "CLOSE_WRITE e:" + e.getMessage());
                            } finally {
                                CloseableUtil.close(fileOutputStream);
                            }
                            // 这里要传截图的原路径(清除原图片)
                            MediaScannerConnection.scanFile(mContext, new String[]{VEnvironment.getScreenshotsSrcFile(path).getPath()}, null, null);
                        
                        }
                    }).start();
            }
        }
    }
    

    我在服务onCreate的时候对其进行初始化监听

    public class FileObserverService extends Service {
        private static final String TAG = "FileObserverService";
        private SDCardListener listener;
    
    
        @Nullable
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            if (listener == null) {
                Log.i(TAG, "FileObserverService startWatching");
                listener = new SDCardListener(VEnvironment.getOutScreenshots(), getApplicationContext());
                //开始监听
                listener.startWatching();
            }
            return super.onStartCommand(intent, flags, startId);
        }
    
        @Override
        public void onDestroy() {
            super.onDestroy();
            if (listener != null) {
                Log.i(TAG, "FileObserverService stopWatching");
                listener.stopWatching();
                listener = null;
            }
    
        }
    }
    

    这样就完成了。

    相关文章

      网友评论

        本文标题:【Android】监听文件的创建

        本文链接:https://www.haomeiwen.com/subject/gnwdkctx.html