1

I am unable to download a file to a specified path on an Android phone from the server, but nothing happens when running this code.
I have used all the required permissions in the manifest file:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

This is my MainActivity class:

private final String PATH = "/storage/sdcard0/BT/";


public void DownloadFromUrl(String fileName1) {  //this is the downloader method
    try {

        URL url = new URL(f);

        File file = new File(fileName1);


        HttpURLConnection ucon = (HttpURLConnection) url.openConnection();

        ucon.setRequestMethod("GET");
        ucon.setDoOutput(true);
        ucon.connect();
        FileOutputStream fos = new FileOutputStream(file);

        InputStream is = ucon.getInputStream();

        BufferedInputStream bis = new BufferedInputStream(is);


        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(file);
        fos.write(baf.toByteArray());
        fos.close();
       // Log.d("ImageManager", "download ready in"
         //       + ((System.currentTimeMillis() - startTime) / 1000)
           //     + " sec");

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

    }
Phantômaxx
  • 37,352
  • 21
  • 80
  • 110
priyanka
  • 161
  • 12
  • 2
    What error are you getting? Do you see any stack traces in logcat? – tambykojak Apr 17 '15 at 17:45
  • 1
    Welcome to Stack Overflow! I have edited your question a bit. You could improve it further by adding the relevant LogCat or server logs. Also, I removed the "any" tag, if you read the description of that tag you will see that it is about testing. – S.L. Barth Apr 17 '15 at 19:14
  • You can get your answer here : http://stackoverflow.com/questions/3028306/download-a-file-with-android-and-showing-the-progress-in-a-progressdialog?rq=1 – silent_programmer Apr 17 '15 at 19:15
  • How are you calling into this code? Add some intentional logging at each stage so that you can see that partial progress is made. – Chris Stratton Apr 17 '15 at 19:31
  • I am not getting any error in logcat,instead of this progress bar process continues infinitely.... – priyanka Apr 18 '15 at 05:06
  • Thnaks issue resolved by using the concept of ASYNCTASK – priyanka Apr 18 '15 at 08:02

1 Answers1

1

In Android, you can't have network related tasks run on the main thread, the guidelines specify that the main thread is meant for UI tasks.

In order to perform network actions, such as downloading files, performing GET and POST requests and other various requests, you need to use AsyncTask

There are many ways to implement AsyncTask, and you can find many examples on Stack Overflow and also in other sites, I'll link a few below:

Please refer to the guides I supplied to get an idea on how to use AsyncTask to perform long running tasks in the app's background.

It shouldn't be too hard to use after you figure out the basic concept

Community
  • 1
  • 1
Mor Paz
  • 2,060
  • 2
  • 19
  • 37