views:

64

answers:

1

I have a video file on a website in .MP4 format and I want to allow the user to be able to download the video to their SD card by clicking a link. Is there an easy way to do this. I currently have this code but its not working...not sure what I am doing wrong. THanks for any help!

import java.io.BufferedInputStream;

import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection;

import org.apache.http.util.ByteArrayBuffer;

import android.app.Activity; import android.os.Bundle; import android.util.Log;

public class VideoManager extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);}

    private final String PATH = "/sdcard/download/";  //put the downloaded file here


    public void DownloadFromUrl(String VideoURL, String fileName) {  //this is the downloader method
            try {
                    URL url = new URL("http://www.ericmoyer.com/episode1.mp4"); //you can write here any link
                    File file = new File(fileName);

                    long startTime = System.currentTimeMillis();
                    Log.d("VideoManager", "download begining");
                    Log.d("VideoManager", "download url:" + url);
                    Log.d("VideoManager", "downloaded file name:" + fileName);
                    /* Open a connection to that URL. */
                    URLConnection ucon = url.openConnection();

                    /*
                     * Define InputStreams to read from the URLConnection.
                     */
                    InputStream is = ucon.getInputStream();
                    BufferedInputStream bis = new BufferedInputStream(is);

                    /*
                     * Read bytes to the Buffer until there is nothing more to read(-1).
                     */
                    ByteArrayBuffer baf = new ByteArrayBuffer(50);
                    int current = 0;
                    while ((current = bis.read()) != -1) {
                            baf.append((byte) current);
                    }

                    /* Convert the Bytes read to a String. */
                    FileOutputStream fos = new FileOutputStream(PATH+file);
                    fos.write(baf.toByteArray());
                    fos.close();
                    Log.d("VideoManager", "download ready in"
                                    + ((System.currentTimeMillis() - startTime) / 1000)
                                    + " sec");

            } catch (IOException e) {
                    Log.d("VideoManager", "Error: " + e);
            }

    }

}

+3  A: 

Never hardwire a path, particularly to external storage. Your path is wrong on many devices. Use Environment.getExternalStoragePath() to get the root of external storage (which may be /sdcard or /mnt/sdcard or something else).

Be sure to create your subdirectory, using the File object you get back from Environment.getExternalStoragePath().

And, finally, don't just say "but its not working". We have no idea what "but its not working" means in your case. Without that information, it is very difficult to help you.

CommonsWare
Thank you so much. I'll remember that next time.
Jogan