美文网首页
USB插入后自动弹出自定义文件管理器,点击文件自动拷贝到本地

USB插入后自动弹出自定义文件管理器,点击文件自动拷贝到本地

作者: android_Joe | 来源:发表于2018-11-02 11:14 被阅读2次

    需求:当USB插入是获取U盘上的文件目录,自动弹出文件管理,点击文件拷贝到本地,自动用WPS自动打开文件
    1、静态注入广播FileListBroadcastReceiver

    <receiver android:name=".FileListBroadcastReceiver">
    
            <action android:name="android.intent.action.MEDIA_MOUNTED"/>
    
            <action android:name="android.intent.action.MEDIA_EJECT"/>
    
            <action android:name="android.intent.action.MEDIA_REMOVED"/>
    
            <data android:scheme="file"/>
    
    </receiver>
    

    2、监听USB插入广播

    @Override
    
    public void onReceive(Context context, Intent intent) {
    
    if (intent.getAction().equals(Intent.ACTION_MEDIA_MOUNTED)) {
    
    String path = intent.getData().getPath();
    
            File file =new File(path);
    
            if (TextUtils.equals(file.getAbsolutePath(), Environment.getExternalStorageDirectory().getAbsolutePath())) {
    
    return;
    
            }
    
    // 插入usb
    
            Intent intent2 =new Intent(context, MainActivity.class);
    
            intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    
            intent2.putExtra("path", path);
    
            context.startActivity(intent2);
    
            if(mUsbResponseListener !=null){
    
    mUsbResponseListener.onInsertUsb(path, true);
    
            }
    
    isFinished =false;
    
        }else if (intent.getAction().equals(Intent.ACTION_MEDIA_EJECT)
    
    || intent.getAction().equals(Intent.ACTION_MEDIA_REMOVED)) {
    
    // 拔出usb
    
            String path = intent.getData().getPath();
    
            if(mUsbResponseListener !=null){
    
    mUsbResponseListener.onInsertUsb(path, false);
    
            }
    
    isFinished =true;
    
        }
    
    }
    

    3、读取文件目录

    class FileListTaskextends AsyncTask> {
    
    StringusbPath;
    
        public FileListTask(String usbPath) {
    
    this.usbPath = usbPath;
    
        }
    
    @Override
    
        protected ListdoInBackground(Integer... integers) {
    
    File file =new File(usbPath);
    
            currentFilePath = file;
    
            final List docTypes = Arrays.asList(getResources().getStringArray(R.array.docType));
    
            final List pptTypes = Arrays.asList(getResources().getStringArray(R.array.pptType));
    
            if (!file.isDirectory()) {
    
    return null;
    
            }
    
    List details =new ArrayList<>();
    
            File[] files = file.listFiles(new FilenameFilter() {
    
    @Override
    
                public boolean accept(File dir, String filename) {
    
    if (new File(dir.getPath(), filename).isDirectory() && !new File(dir.getPath(), filename).isHidden()) {
    
    return true;
    
                    }
    
    if (docTypes.contains(FileUtls.getFileSuffix(filename)) ||pptTypes.contains(FileUtls.getFileSuffix(filename))) {
    
    return true;
    
                    }
    
    return false;
    
                }
    
    });
    
            if (files ==null) {
    
    return null;
    
            }
    
    for (File f : files) {
    
    if (!f.isHidden()) {
    
    // 隐藏文件夹或者文件不显示
    
                    FileDetail fd =new FileDetail();
    
                    fd.fileName = f.getName();
    
                    fd.selected =false;
    
                    fd.filePath = f.getAbsolutePath();
    
                    if (f.isDirectory()) {
    
    fd.icon = R.mipmap.ic_file_floder_icon;
    
                        f.listFiles(new FilenameFilter() {
    
    @Override
    
                            public boolean accept(File dir, String filename) {
    
    if (new File(dir.getPath(), filename).isDirectory() && !new File(dir.getPath(), filename).isHidden()) {
    
    return true;
    
                                }
    
    if (docTypes.contains(FileUtls.getFileSuffix(filename))
    
    ||pptTypes.contains(FileUtls.getFileSuffix(filename))) {
    
    return true;
    
                                }
    
    return false;
    
                            }
    
    });
    
                    }else {
    
    if (docTypes.contains(FileUtls.getFileSuffix(f.getAbsolutePath()))) {
    
    fd.icon = R.mipmap.ic_word;
    
                        }else if (pptTypes.contains(FileUtls.getFileSuffix(f.getAbsolutePath()))) {
    
    fd.icon = R.mipmap.ic_ppt;
    
                        }
    
    }
    
    details.add(fd);
    
                }
    
    }
    
    Collections.sort(details, new Comparator() {
    
    @Override
    
                public int compare(FileDetail lhs, FileDetail rhs) {
    
    File lf =new File(lhs.filePath);
    
                    File rf =new File(rhs.filePath);
    
                    if (lf.isDirectory()) {
    
    return -1;
    
                    }else if (!rf.isDirectory()) {
    
    return 0;
    
                    }else {
    
    return 1;
    
                    }
    
    }
    
    });
    
            return details;
    
        }
    
    @Override
    
        protected void onPostExecute(List fileDetails) {
    
    super.onPostExecute(fileDetails);
    
            setPath(rootPath, usbPath);
    
            if (fileDetails ==null) {
    
    return;
    
            }
    
    mFileList = fileDetails;
    
            mFileListAdapter.setData(mFileList);
    
        }
    
    }
    

    4、点击文件把文件拷贝到本地目录

    if (new File(file.filePath).isDirectory()) {
    
    new FileListTask(file.filePath).execute();
    
    return;
    
    }
    
    // 拷贝文件到本地
    
    File localFile =new File(getDir(), file.fileName);
    
    if (localFile.exists()) {
    
    try {
    
    localFile.createNewFile();
    
        }catch (IOException e) {
    
    e.printStackTrace();
    
        }
    
    }
    
    FileUtls.copyFile(file.filePath, localFile.getPath());
    
    FileUtls.openFile(this, localFile.getPath());
    

    源码地址:https://github.com/123yangu/USBFileList

    相关文章

      网友评论

          本文标题:USB插入后自动弹出自定义文件管理器,点击文件自动拷贝到本地

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