android 应用共享文件, 客户端接收后处理intent路径

谷歌标准文档: https://developer.android.com/training/secure-file-sharing/request-file.html

感谢博文:https://blog.csdn.net/lmj623565791/article/details/72859156

网上找了半天 怎么处理应用共享的 intent 文件路径,但晚上都是处理图片什么的, 没有直接回答怎么处理对应文本路径,  比如用QQ, 微信 打开某文件, 这时你选自己的应用, 应用启动后会传来一个 intent 地址,  这时由于7.0后的权限问题,你是不能直接读取修改的, 其实很简单, 可能是我java基础太差, 不过最后从google中读取到了结果, 直接上代码:

//这里传入你的intent内容        
Uri returnUri = intent.getData();
        /*
         * Try to open the file for "read" access using the
         * returned URI. If the file isn't found, write to the
         * error log and return.
         */
        try {
            /*
             * Get the content resolver instance for this context, and use it
             * to get a ParcelFileDescriptor for the file.
             */
            inputPFD = getContentResolver().openFileDescriptor(returnUri, "r");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            Log.e("MainActivity", "File not found.");
            return;
        }
        // Get a regular file descriptor for the file
        FileDescriptor fd = inputPFD.getFileDescriptor();

//复制文件
        try {
            FileInputStream instrm = new FileInputStream(fd);

            FileOutputStream fs = new FileOutputStream(getExternalFilesDir("").getPath()+"/" + "jjj.xlsx");
            byte[] buffer = new byte[1024];
            int length;
            int byteread = 0;
            int bytesum = 0;
            while ((byteread = instrm.read(buffer)) != -1) {
                bytesum += byteread; //字节数 文件大小
                //System.out.println(bytesum);
                fs.write(buffer, 0, byteread);
            }
            fs.flush();
            fs.close();
            instrm.close();
        }
        catch (Exception e){
            Log.e("dfdf", " read error "+e.toString());
        }

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值