gpt4 book ai didi

android - 文件在 sdcard/download 文件夹中不存在

转载 作者:行者123 更新时间:2023-11-30 02:29:44 24 4
gpt4 key购买 nike

我正在使用以下代码从网站下载文件。但是当我转到sdcard/download文件夹时,该文件不存在。

if(et1.equals(example)){

try {
//this is the file you want to download from the remote server
String path ="http://lifecraze.com/sercice_tax_faq.zip";
//this is the name of the local file you will create
String targetFileName = "sercice_tax_faq.zip";
boolean eof = false;
URL u = new URL(path);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
FileOutputStream f = new FileOutputStream(new File("sdcard/download/"+targetFileName));
InputStream in = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ( (len1 = in.read(buffer)) > 0 ) {
f.write(buffer,0, len1);
}
f.close();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Intent it=new Intent(Example.this,Sucess.class);
startActivity(it);
}
else{
Intent it=new Intent(Example.this,Failed.class);
startActivity(it);
}
}
});

请帮助我如何获取sdcard/download文件夹中的文件。

提前致谢

最佳答案

代替:

new File("sdcard/download/"+targetFileName));

尝试:

new File("/sdcard/download", targetFileName));

注意前导斜杠。你想要一个绝对路径。

但是,这样更好,因为这样我们就不会对 SD 卡的安装位置做出假设。

new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString(), targetFileName);

关于android - 文件在 sdcard/download 文件夹中不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27467271/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com