Question

Here is my code:--

public class grabber extends Activity 
{


int counter;
String folderPath;
String[] array;
DownloadManager dm;
private long enqueue;

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.data);

    Down t = new Down();
    t.execute();




}


private class Down extends AsyncTask<String, Void, String> 
{
    protected String doInBackground(String... params)
    {
        Log.i("grabber", "in back");
String   w =Environment.getExternalStorageDirectory().getAbsoluteFile()+"/Android/data/myfolder";
    createdir(w);


    String link = "http://mywebsitehere.com";


    Document doc;

      array = new String[200];
    try {

        doc = Jsoup.connect(link).get();
        String title = doc.title();


         folderPath = w+ File.separator+title;

         createdir(folderPath);


        Elements images = doc.select("img[src~=(?i)\\.(png|jpe?g|gif)]");
         counter =0;

        for (Element image : images) 
        {
             String img = image.attr("src");
            array[counter]=img;

            counter++;
        }

    } catch (IOException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}


    protected void onPostExecute(String result) 
    {   

        Toast.makeText(getApplicationContext(), 
          "done", Toast.LENGTH_SHORT).show();

        for(int i=0;i<counter;i++)
        {
            dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
            Request request = new Request(
                    Uri.parse(array[i]));
            request.setDescription("Android Data download using DownloadManager.");
              request.setDestinationInExternalFilesDir(getApplicationContext(),folderPath,File.separator+"pic"+Integer.toString(i)+".jpg");
            enqueue = dm.enqueue(request);



        }


    }
 }




public void createdir(String Path)
{
File file = new File(Path);
    if(!file.exists())
    {
        file.mkdirs();

    }

}
}

What it should do:- It should download the given links, which contains images, to the given path.

What it does:- The app is running quite fine, my UI is not hanging. After a few second "done" Toast is shown, which means theasynctask is completed and we are at onPostExecute. But when I check the dir I am seeing that there is no folder inside Android/data/myfolder. The main setback is that some times it works, i.e. the images are downloaded and sometimes not.

So I am guessing maybe its because of some http timeout or some error which I am not handling correctly because I am using createdir(folderPath); after Jsoup and maybe the error is happening there so no folder is getting created there.

Any Ideas!?? Thanks in Advance.

Edit:- I am getting SocketTimeoutException during jsoup.connect(link).get();

Was it helpful?

Solution 2

Finally I did it with some trial and error.

We can use timeout option here to reduce the Sockettimeoutexception by changing

        doc = Jsoup.connect(link).get();

to

        doc = Jsoup.connect(link).timeout(10000).get();

And there is still one other error, thats to remove the Toast message in the onPostExecute as we cannot interact without handler from background task to UI.

That's it.

OTHER TIPS

You have to add the following permission to the manifest of your application, to write to the external storage.

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

I hope you did this?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top