我试图在 Android 路径出现时复制该文件,但没有成功,尽管它已经作为 AndroidManifest 上的权限存在,就好像该文件不存在或在那里不知道它一样。这是一段代码:public void onActivityResult(int requestCode, int resultCode, Intent data){ switch (requestCode) { case FILE_SELECT_CODE: if (resultCode == RESULT_OK) { Uri uri = data.getData(); String origem = uri.getPath(); File src = new File(origem); if(src.isFile()){ Log.d(TAG,src.toString()); } File root = Environment.getExternalStorageDirectory(); root = new File(root.toString(), "Kcollector/importa/import.csv"); try { copy(src,root,true); } catch (IOException e) { e.printStackTrace(); } } break; } super.onActivityResult(requestCode, resultCode, data);}public static void copy(File origem, File destino, boolean overwrite) throws IOException{ Date date = new Date(); if (destino.exists() && !overwrite){ System.err.println(destino.getName()+" já existe, ignorando..."); return; } FileInputStream fisOrigem = new FileInputStream(origem); FileOutputStream fisDestino = new FileOutputStream(destino); FileChannel fcOrigem = fisOrigem.getChannel(); FileChannel fcDestino = fisDestino.getChannel(); fcOrigem.transferTo(0, fcOrigem.size(), fcDestino); fisOrigem.close(); fisDestino.close(); Long time = new Date().getTime() - date.getTime(); System.out.println("Saiu copy"+time);}尝试复制时返回的错误:W/System.err:java.io.FileNotFoundException:/external_storage/Kcollector/importa/kvendasajustado.csv(没有这样的文件或目录) W/System.err:在 java.io.FileInputStream.open0(本机方法)
1 回答
偶然的你
TA贡献1841条经验 获得超3个赞
检查运行时权限或使用第一个响应中指示的方法
public static void copy(File src, File dst) throws IOException {
InputStream in = new FileInputStream(src);
try {
OutputStream out = new FileOutputStream(dst);
try {
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
} finally {
out.close();
}
} finally {
in.close();
}
}
最后检查 url 是否格式正确。否则你可以用断点调试(搜索这些词),并找到问题的确切来源
添加回答
举报
0/150
提交
取消