0

I'm still new to the whole coding thing, but I would like some help with ASyncTask and how to allow it to download my app from the internet straight to my phone through notification or just in the SD Card.

I already have a button inside the activity_main.xml layout which is:

<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/UpdateButton"
    android:text="@string/Update"
    android:background="#6c0005"
    android:textColor="#ffffff"
    android:textSize="12dp" />

In my MainActivity.Java class I just have right now a button connecting to a string and intent to redirect it to a webview to download it:

case R.id.UpdateButton:
    Intent browserIntent = new Intent(Intent.ACTION_VIEW,
    Uri.parse(getString(R.string.UpdateAV)));
    startActivity(browserIntent);
    break;

but instead of that, I would like the file to download straight to the phone or a progress bar saying it is downloading to the External memory or SD Card.

Any help would be appreciated.

JRMVN
  • 13
  • 5
  • 3
    Possible duplicate of [Download a file with Android, and showing the progress in a ProgressDialog](http://stackoverflow.com/questions/3028306/download-a-file-with-android-and-showing-the-progress-in-a-progressdialog) – DZDomi Dec 30 '15 at 17:43

1 Answers1

0

this is just an example that how you can download a file, you would have to write an equivalent code for this in Android if you want to use AsyncTask, in which you will do the same work in the doInBacground() method.

public class DownloadTest {

public static void main(String[] args) {

    Thread thread = new Thread(){
        @Override
        public void run() {
            // TODO Auto-generated method stub
            super.run();
            try {
                dowanloadFile(new URL("some url"), new File("some file"));
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    };
    thread.start();


   }

private static void dowanloadFile(URL url, File file){
    try {
        FileUtils.copyURLToFile(url, file );
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}

this code code uses commons-io-2.4.jar library,

you can get little more code her Example

you can get path to download at SD card like this

final String path = Environment.getExternalStorageDirectory().getAbsolutePath();

with AsycnTask it would be like this

private class MtDownloadTask extends AsyncTask<Void, Void ,Void>{

    URL someUrl = new URL("your url String"); //
    File someFile = new File("your file, where you want to save the data");

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(Void... params) {
        return null;
        FileUtils.copyURLToFile(url, file);
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
    }

}

make sure you do have internet permission to your app, to do that add this line to your AndroidManifest.xml

    <uses-permission android:name="android.permission.INTERNET"/>
Community
  • 1
  • 1
Pankaj Nimgade
  • 4,529
  • 3
  • 20
  • 30
  • Thank you for the answer. Do I put this in the MainActivity? Or a different java class. Also, where would I put the .jar file? I'm not sure. – JRMVN Dec 30 '15 at 18:22
  • @JRMVN you would have to add the jar file in the **libs** directory of your project – Pankaj Nimgade Dec 30 '15 at 18:27
  • I have added the code for asycTask you can make that as inner class to the MainActivity or you can have it as a separate class whichever suitable to you. – Pankaj Nimgade Dec 30 '15 at 18:29
  • 1
    Thank you Pankaj! Really appreciate the fast help. I'll let you know how I go on :). – JRMVN Dec 30 '15 at 21:16
  • I've added the first codes into my main activity java class, I've also added the .jar file to the libs folder. Should I add the ASyncTask to the MainActivity.java class too? Also when I do, it highlights 'onPreExecute' and 'onPostExecute' red. Also 'public static void main(String[] args) {' stays red unless it is changed to this 'public static class DownloadTest {' the start of the code? I'm quite confused here, sorry. – JRMVN Dec 30 '15 at 21:49
  • @JRMVN, the first example was to show how the download method is called in Java, It was written for plain java project for your understanding – Pankaj Nimgade Dec 31 '15 at 09:42
  • @JRMVN, Once you add a library to the Project by putting the file under **libs** directory you have to add that as library to your project so the Project will use appropriate classes rendered by the library you have added to the project – Pankaj Nimgade Dec 31 '15 at 09:43
  • @JRMVN, you should check some tutorial for how people are using **AsyncTask**, and then check http://developer.android.com/training/basics/data-storage/files.html, you will get the idea what should be done :) – Pankaj Nimgade Dec 31 '15 at 09:46