On Jan 20, 5:45 pm, rahul verma <rahulsgonnar...@gmail.com> wrote:
> hi everyone,
> i am caught in a big problem...
>
> I have made a downloading app. it works fine ,but one big problem is
> there.
>
> When i am downloading a .apk file using my code,on installing the
> downloaded file i am always getting a package parsing error
>
> My code:
>
>  try {
>                                 URL url = new URL(link);
>                                 HttpURLConnection connection;
>                                 try {
>                                         connection = (HttpURLConnection) 
> url.openConnection();
>                                          File file=new 
> File(SDCardDir+File.separator
> +"DownloadEverythingPro"+File.separator+fileName);
>                                         if(file.exists()){
>                                              downloaded = (int) file.length();
>                                              status=Handlerr.RESUMED;
>                                              
> connection.setRequestProperty("Range", "bytes="+
> (file.length())+"-");
>
>                                         }
>                                     else{
>                                         downloaded=0;
>                                         
> connection.setRequestProperty("Range", "bytes=" + 0 +
> "-");
>                                         status=Handlerr.FRESH_DOWNLOAD;
>
>                                     }
>                                         connection.setDoInput(true);
>                                         connection.setDoOutput(true);
>                                         j=connection.getContentLength();
>                                         int lastSlash = 
> url.toString().lastIndexOf('/');
>                                         fileName = "file.bin";
>                                         if(lastSlash >=0)
>                                         {
>                                             fileName = 
> url.toString().substring(lastSlash + 1);
>                                         }
>                                         if(fileName.equals(""))
>                                         {
>                                             fileName = "file.bin";
>                                         }
>
>                                         k=j+downloaded;
>                                         BufferedInputStream in = new
> BufferedInputStream(connection.getInputStream());
>                                         FileOutputStream fos=(downloaded==0)? 
> new
> FileOutputStream(SDCardDir+File.separator
> +"DownloadEverythingPro"+File.separator+fileName): new
> FileOutputStream(SDCardDir+File.separator
> +"DownloadEverythingPro"+File.separator+fileName,true);
>                                         BufferedOutputStream bout = new 
> BufferedOutputStream(fos,
> 1024);
>                                        byte[] data = new byte[1024];
>                                        int x = 0;
>                                        while ((x = in.read(data, 0, 1024)) >= 
> 0) {
>                                        bout.write(data, 0, x);
>
>                                             downloaded += x;
>                                             
> informMiddle(status+"!"+downloaded+"/"+k
> +"!"+this.fileName+"!"+this.link2);
>                                        }
>                                        in.close();
>                                        fos.close();
>                                        bout.close();
>                                 } catch (IOException e) {
>                                         // TODO Auto-generated catch block
>                                         e.printStackTrace();
>                                         return;
>                                 }
>
>                         } catch (MalformedURLException e) {
>                                 // TODO Auto-generated catch block
>                                 e.printStackTrace();
>                                 return;
>
>                         }
\

 well well well .... After trying to search for an answer online,
wasting hours trying myself and finally slamming my head on a wall i
found the reason of error

replacing in.read(data, 0, 1024) by in.read(data) , the downloaded apk
now installs normally ... fewwwwwww.....

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to