我正在开发一个应用程序,列出目录中的文件数组,然后允许用户打开/下载列出的任何文件。知道我应该从哪里开始吗?
答案 0 :(得分:0)
你可以试试它的代码:
private class File_Video extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... sUrl) {
File folders = new File(Environment.getExternalStorageDirectory()
+ "/pathofthefile/");
folders.mkdirs();
File file;
file = new File(Environment.getExternalStorageDirectory()
+ "/pathofthefile/file_video.txt");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
file.delete();
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
URL url = new URL(sUrl[0]);
URLConnection connection = url.openConnection();
connection.connect();
// this will be useful so that you can show a typical 0-100%
// progress bar
int fileLength = connection.getContentLength();
// download the file
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(file);
byte data[] = new byte[1024];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
publishProgress((int) (total * 100 / fileLength));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
return "Downloaded";
} catch (Exception e) {
return null;
}
}
来自onCreate:
File_Video downloadFile_video = new File_Video();
downloadFile_image.execute("http://youvebsite.com/file1.txt");