谷歌标准文档: 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());
}