Friday, 1 June 2012

Download a file from URL to external Card in android


 void downloadFile(String fileUrl)
    {  
    URL myFileUrl = null;
    try
    {  
    myFileUrl = new URL(fileUrl);
    }
    catch (MalformedURLException e)
    {      
    e.printStackTrace();  
    }  
    Bitmap bmImg = null;
try {  
    HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();  
    conn.setDoInput(true);  
    conn.connect();    
    int length = conn.getContentLength();
    InputStream is = conn.getInputStream();
    bmImg = BitmapFactory.decodeStream(is);
    }
      catch (IOException e)
    {      
    e.printStackTrace();  
    }  
    try {      
    String filepath=Environment.getExternalStorageDirectory().getAbsolutePath();
    FileOutputStream fos = new FileOutputStream(filepath + "/" + "abc.jpg");
    bmImg.compress(CompressFormat.JPEG, 75, fos);  
    fos.flush();    
    fos.close();
    MessageBox("Image Download Sucessfull");
    }
    catch (Exception e)
    {  
    e.printStackTrace();  
    }
    }

No comments:

Post a Comment