Android Studio 通过系统文件管理器浏览文件和文件复制(只模拟器实现)

一.通过系统文件管理器浏览(选择)文件

1.使用 Intent 调用系统文件管理器。

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");//无类型限制,
/*
(“image/*”);//选择图片
(“audio/*”); //选择音频
(“video/*”); //选择视频 
(“video/*;image/*”);//同时选择视频和图片
*/
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent, 1);

2.获取返回值,进行相应想要的操作

    @SuppressLint({"MissingSuperCall", "NewApi"})
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == 1) {
                String path;
                Uri uri = data.getData();
                path = uri.getPath();
                File file = new File(path);
                //想要对文件的操作
                //.......
            }
        }
    }

二.文件复制

java中的四种文件复制的方法:
1.

private static void copyFile(File source, File dest)
    throws IOException {
            Files.copy(source.toPath(), dest.toPath());
    }

2.

private static void copyFile(File source, File dest)
    throws IOException {
        InputStream input = null;
        OutputStream output = null;
        try {
            input = new FileInputStream(source);
            output = new FileOutputStream(dest);
            byte[] buf = new byte[1024];
            int bytesRead;
            while ((bytesRead = input.read(buf)) > 0) {
                output.write(buf, 0, bytesRead);
            }
        } finally {
            input.close();
            output.close();
        }
    }

3.

    @RequiresApi(api = Build.VERSION_CODES.Q)
    private static void copyFile(File source, File dest)
            throws IOException {
            FileUtils.copy(new FileInputStream(source), new FileOutputStream(dest));
    }

4.

private void copyFile(File source, File dest) throws IOException {
        FileChannel inputChannel = null;
        FileChannel outputChannel = null;
        try {
            inputChannel = new FileInputStream(source).getChannel();
            outputChannel = new FileOutputStream(dest).getChannel();
            outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
        } finally {
            inputChannel.close();
            outputChannel.close();
        }
    }

三.总结

       通过以上的方法即可在理论上实现选择指定的文件并将其复制到指定的位置,为什么是理论上的原因是,在模拟器上,使用以上的方法可以成功的实现对文件的操作,但是在手机上就有些问题了,虽然给了app读写权限,但是仍然无法实现将自己选择的文件复制到指定的位置,这就是我为什么我上面列出了四种文件复制的方法,在真机(手机)上,我无论使用哪种复制方式都无法实现文件复制,最后仔细看报错后发现是我想要复制的那个文件总是是打开失败,访问不到(文件存在且路径正确),我认为这个还是权限问题,我是想将其他app中的文件复制到自己的app下,这可能涉及到对其他app文件的访问权限,所以报错为复制的目标文件无法打开,访问不到。具体报错如下,如有大佬解决,希望得到指教。

在模拟器上不会有下面两句,在真机就会出现:

E/BehaviorCollectManager: Fail to acquire dataAnalyzerService...

E/AwareLog: AtomicFileUtils: readFileLines file not exist: android.util.AtomicFile@8c8bdae

报错:

......./5099321.txt: open failed: ENOENT (No such file or directory)

       而且,我直接使用 file.exists() 去判断指定路径下的文件是否存在,在手机运行时得到的是:不存在该文件,但是在手机上文件是存在的。

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值