Juraj Hodoň
Juraj Hodoň

Reputation: 3

Downloading files to Android

I'm writing an application and later on I will need to write a code for downloading file(s) in to the Android device from remote server. I can connect to a server using JSch library but I have no idea how to actually download a file. Any tips on what should I look for or where should I start? Thanks

Upvotes: 0

Views: 909

Answers (2)

Uriel Arvizu
Uriel Arvizu

Reputation: 1916

You could try with something like this:

    Session session = null;
    Channel channel = null;
    ChannelSftp channelSftp = null;
    boolean success = false;
    JSch jsch = new JSch();
    session = jsch.getSession("your_username", "your_hostname", "your_port");
    session.setPassword("your_password");

    session.setConfig("StrictHostKeyChecking", "no");
    session.connect();

    channel = session.openChannel("sftp");
    channel.connect();
    channelSftp = (ChannelSftp) channel;

    String fileToDownload = "some_url_to_some_file";

    channelSftp.get(fileToDownload , "you_destination_path");
    success = true;
    if (success)
        System.out.println("Downloaded file: " + fileToDownload );

Of course if you were to have a list of file names to download you'd had to iterate through it, also you'd need to add the necessary exception handling if something goes wrong.

Upvotes: 0

LaurentY
LaurentY

Reputation: 7653

There's a specific service in Android to help you to download file.

It's DownloadManager : http://developer.android.com/reference/android/app/DownloadManager.html

There's a tutorial at : http://blog.vogella.com/2011/06/14/android-downloadmanager-example/

Upvotes: 1

Related Questions