[英]Android, Download a .txt file and save internally
like the title states i am simply trying to download a test.txt file, the following url and save it internally, ideally within drawable. 就像标题说明一样,我只是想下载一个test.txt文件,以下网址并将其内部保存,最好是在drawable中保存。
i have been trying to modify this to work but will little success i keep getting "unable to download null" errors 我一直在尝试修改它以使其正常工作,但收效甚微,但我不断收到“无法下载null”错误
int count;
try {
URL url = new URL("https://www.darkliteempire.gaming.multiplay.co.uk/testdownload.txt");
URLConnection conexion = url.openConnection();
conexion.connect();
int lenghtOfFile = conexion.getContentLength();
InputStream is = url.openStream();
File testDirectory = new File(Environment.getExternalStorageDirectory() + "/Download");
if (!testDirectory.exists()) {
testDirectory.mkdir();
}
FileOutputStream fos = new FileOutputStream(testDirectory + "/test.txt");
byte data[] = new byte[1024];
long total = 0;
int progress = 0;
while ((count = is.read(data)) != -1) {
total += count;
int progress_temp = (int) total * 100 / lenghtOfFile;
fos.write(data, 0, count);
}
is.close();
fos.close();
} catch (Exception e) {
Log.e("ERROR DOWNLOADING", "Unable to download" + e.getMessage());
}
There must be a simpler way to do this? 必须有一个更简单的方法来做到这一点吗? the file itself is tiny with perhaps 3 or 4 lines of text so i dont need anything fancy 该文件本身很小,可能只有3或4行文本,所以我不需要任何花哨的东西
Please Update your below code line and write valid url. 请更新您的以下代码行并输入有效的网址。
URL url = new URL("https://www.http://darkliteempire.gaming.multiplay.co.uk/testdownload.txt");
after write valid url your code line look like this. 写入有效的网址后,您的代码行如下所示。
URL url = new URL("http://www.darkliteempire.gaming.multiplay.co.uk/testdownload.txt");
it will solve your problem. 它会解决您的问题。
Using AQuery library you get something pretty straightforward. 使用AQuery库,您会得到一些非常简单的信息。 Plus you'll get hips of other cool functions to shorten your code. 另外,您还将获得其他一些很棒的功能来缩短代码。
http://code.google.com/p/android-query/wiki/AsyncAPI http://code.google.com/p/android-query/wiki/AsyncAPI
String url = "https://picasaweb.google.com/data/feed/base/featured?max-results=16";
File ext = Environment.getExternalStorageDirectory();
File target = new File(ext, "aquery/myfolder/photos.xml");
aq.progress(R.id.progress).download(url, target, new AjaxCallback<File>(){
public void callback(String url, File file, AjaxStatus status) {
if(file != null){
showResult("File:" + file.length() + ":" + file, status);
}else{
showResult("Failed", status);
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.