Kris Kris - 2 months ago 17
Android Question

Android: How to download file in android?

I'm trying to download a file from a URL. I have the following code.

package com.example.downloadfile;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

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 {
//this is the file you want to download from the remote server
String path ="http://www.fullissue.com/wp-content/uploads/2010/12/Adam-Lambert.jpg";
//this is the name of the local file you will create

String targetFileName = "al.jpg";

boolean eof = false;

URL u = new URL(path);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();

String PATH_op = Environment.getExternalStorageDirectory() + "/download/" + targetFileName;

tv.append("\nPath > " + PATH_op);

FileOutputStream f = new FileOutputStream(new File(PATH_op));

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();
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
}

tv.append("\nAnother append!");
this.setContentView(tv);
}

}


Can someone tell me what's wrong with this code. I'm not able to see the file I'm supposed to download. I'm new to java and android dev, many thanks for any help. :)

Answer
  1. You are doing network I/O on the main application thread. At best, this will freeze your UI while the download is going on. At worst, you activity will crash with an "Application Not Responding" (ANR) dialog.

  2. You are trying to write to a directory (download/) on external storage that might not exist. Please create the directory first.

  3. Please consider switching to using Log.e() for your error logging, as I do not know if printStackTrace() works on Android.

Also, make sure that you have the WRITE_EXTERNAL_STORAGE permission and that you are using adb logcat, DDMS, or the DDMS perspective in Eclipse to examine LogCat and look for the error messages you are trying to log.