我们项目的使用场景,手机qq打开文件,选择其他打开方式,选择我们自己的应用打开,通过intent.getData()获取文件地址,后来发现在Android7.0之后的版本,获取到的地址不正确,说文件不存在。日志报错column-data-does-not-exist,

经过一天的折腾,终于在网上找到了一些零碎的知识拼接成了解决办法,具体如下:

判断版本获取路径的方式,在拿到uri之后进行版本判断大于等于24(即Android7.0)用最新的获取路径方式,否则用你之前的方式,
String str =""; if (Build.VERSION.SDK_INT >= 24) { str =
getFilePathFromURI(this, uri);//新的方式 } else { //str = getPath(this,
uri);你自己之前的获取方法 } public String getFilePathFromURI(Context context, Uri
contentUri) { File rootDataDir = context.getFilesDir(); String fileName =
getFileName(contentUri); if (!TextUtils.isEmpty(fileName)) { File copyFile =
new File(rootDataDir + File.separator + fileName); copyFile(context,
contentUri, copyFile); return copyFile.getAbsolutePath(); } return null; }
public static String getFileName(Uri uri) { if (uri == null) return null;
String fileName = null; String path = uri.getPath(); int cut =
path.lastIndexOf('/'); if (cut != -1) { fileName = path.substring(cut + 1); }
return fileName; } public void copyFile(Context context, Uri srcUri, File
dstFile) { try { InputStream inputStream =
context.getContentResolver().openInputStream(srcUri); if (inputStream == null)
return; OutputStream outputStream = new FileOutputStream(dstFile);
copyStream(inputStream, outputStream); inputStream.close();
outputStream.close(); } catch (Exception e) { e.printStackTrace(); } } public
int copyStream(InputStream input, OutputStream output) throws Exception,
IOException { final int BUFFER_SIZE = 1024 * 2; byte[] buffer = new
byte[BUFFER_SIZE]; BufferedInputStream in = new BufferedInputStream(input,
BUFFER_SIZE); BufferedOutputStream out = new BufferedOutputStream(output,
BUFFER_SIZE); int count = 0, n = 0; try { while ((n = in.read(buffer, 0,
BUFFER_SIZE)) != -1) { out.write(buffer, 0, n); count += n; } out.flush(); }
finally { try { out.close(); } catch (IOException e) { } try { in.close(); }
catch (IOException e) { } } return count; }
希望能解决你们遇到的问题。