Search code examples
androidcursorandroid-download-manager

Not able to download the images?


  Cursor appu=X.getsomething();

               if (appu.getCount() > 0)
                  {
                   if  (appu.moveToFirst())
                   {
                     do {
                             String uname = appu.getString(appu.getColumnIndex("content_id"));
                             String ax= appu.getString(appu.getColumnIndex("content"));
                             String unknown =url + ax;
                              t1.append("\n");
                               t1.append(unknown);
                               //downloadimages(unknown,uname); 
                        }while (appu.moveToNext());
                   } 
                   X.close();

this is my code for getting some result from the database and by this result i am creating a url which has the image and i want to download that image for every cursor result when i try to run the downloadimages() function my app crashes always i don't know why ...but when i run the code without it,it runs perfectly.

downloadimages Method

      public void downloadimages(String url,String filename)
        {
          String ur1=url,v1=filename;
            downloadManager = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
              Uri Download_Uri = Uri.parse(ur1);
              DownloadManager.Request request = new DownloadManager.Request(Download_Uri);
              request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
              request.setAllowedOverRoaming(false);
             request.setDescription("Android Data download using DownloadManager.");
           request.setDestinationInExternalFilesDir(getApplicationContext(),Environment.DIRECTORY_DOWNLOADS,v1 + ".jpg");
              downloadManager.enqueue(request);     

        }

please tell me what am i doing wrong???

 Cursor appu=X.getsomething();

               if (appu.getCount() > 0)
                  {
                   if  (appu.moveToFirst())
                   {
                     do {
                             String uname = appu.getString(appu.getColumnIndex("content_id"));
                             String ax= appu.getString(appu.getColumnIndex("content"));
                             imageid.add(uname);
                             String unknown =url + ax;
                             imagepath.add(unknown);
                             // t1.append("\n");
                               //t1.append(unknown);
                        }while (appu.moveToNext());
                   } 
                   X.close();
                  }}
            this.getitdone();
            }


        public void getitdone()
        {
            int b=imagepath.size();

            for(int i=1;i<=b;i++)
            {
                 String extra=imagepath.get(i).toString();
                 String extra2=imageid.get(i).toString();
                downloadimages(extra,extra2);
            }

        }

now i am doing it this way and it is working perfectly


Solution

  • This is how Pramod resolved the issue:

     Cursor appu=X.getsomething();
    
               if (appu.getCount() > 0)
                  {
                   if  (appu.moveToFirst())
                   {
                     do {
                             String uname = appu.getString(appu.getColumnIndex("content_id"));
                             String ax= appu.getString(appu.getColumnIndex("content"));
                             imageid.add(uname);
                             String unknown =url + ax;
                             imagepath.add(unknown);
                             // t1.append("\n");
                               //t1.append(unknown);
                        }while (appu.moveToNext());
                   } 
                   X.close();
                  }}
            this.getitdone();
            }
    
    
        public void getitdone()
        {
            int b=imagepath.size();
    
            for(int i=1;i<=b;i++)
            {
                 String extra=imagepath.get(i).toString();
                 String extra2=imageid.get(i).toString();
                downloadimages(extra,extra2);
            }
    
        }