I'm trying to download a file from a URL. My code doesn't return an error but I can't see the file I'm supposed to download in my internal storage. Here's my code:
package com.example.downloadfile;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.TextView;
public class DownloadFile extends Activity {
private static String fileName = "al.jpg";
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = new TextView(this);
tv.setText("This is download file program... ");
try {
URL url = new URL("http://www.fullissue.com/wp-content/uploads/2010/12/Adam-Lambert.jpg");
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
String PATH = Environment.getDataDirectory() + "/";
tv.append("\nPath > " + PATH);
Log.v("log_tag", "PATH: " + PATH);
File file = new File(PATH);
file.mkdirs();
File outputFile = new File(file, fileName);
FileOutputStream fos = new FileOutputStream(outputFile);
InputStream is = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1) {
fos.write(buffer, 0, len1);
}
fos.close();
is.close();
} catch (IOException e) {
Log.d("log_tag", "Error: " + e);
}
Log.v("log_tag", "Check: ");
tv.append("\nAnother append!");
this.setContentView(tv);
}
}
I'm new to java and android dev, any answers would be much appreciated, thanks!
Yo! I used the ff. code instead. This works for me. Thanks for all your help!
private static String fileName = "beautiful_galaxy - tarantula.jpg";
private static String fileURL = "http://apod.nasa.gov/apod/image/0903/tarantula2_hst_big.jpg";
try {
File root = Environment.getExternalStorageDirectory();
URL u = new URL(fileURL);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
int lenghtOfFile = c.getContentLength();
FileOutputStream f = new FileOutputStream(new File(root + "/download/", fileName));
InputStream in = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
long total = 0;
while ((len1 = in.read(buffer)) > 0) {
total += len1; //total = total + len1
//publishProgress("" + (int)((total*100)/lenghtOfFile));
f.write(buffer, 0, len1);
}
f.close();
} catch (Exception e) {
Log.d("Downloader", e.getMessage());
}
To download a file I use the following code:
If you are downloading file to Sdcard then make sure that your sdcard is mounted, the code will download file to sdcard, if still getting problem let me know.
best of luck :)
Pretty sure (like @dmon said) that you can't write to the data directory in that fashion. You want to:
Environment.getDownloadCacheDirectory ()
orEnvironment.getExternalStorageDirectory ()
. Take a look at the javadoc for http://developer.android.com/reference/android/os/Environment.html#getDataDirectory%28%29.I saw this talk and the guy said you should use the Apache HTTP client and not the java one
Here is a code snippet take from the tutorial:
Check the following permissions in Manifest file:
You will have a NetworkOnMainThreadException now. Look in the log cat. Place your internet code in an asynctask or thread.