Hiển thị các bài đăng có nhãn download_image_file. Hiển thị tất cả bài đăng
Hiển thị các bài đăng có nhãn download_image_file. Hiển thị tất cả bài đăng

Thứ Bảy, 5 tháng 10, 2013

Download file hoặc image trong trong android

try {
          URL url1 = new URL(name);  
              URLConnection con;
              con = url1.openConnection();
              con.setConnectTimeout(10000);
              con.connect();
               
             InputStream input = new BufferedInputStream(con.getInputStream());
       File sdCard = Environment.getExternalStorageDirectory();
       File dir = new File(sdCard.getAbsolutePath() + "/imagene/");
       dir.mkdirs();
       File file = new File(dir, i+".jpg");
       FileOutputStream f = new FileOutputStream(file);
         
         
             byte data[] = new byte[10000];
             
           //  while ((count=input.read(data))!=-1) {
           //    Log.d("phanbom","Da duoc data la:"+count);
                      input.read(data);
                   f.write(data) ;
                 
          //      }
             f.flush();
             f.close();
           
          }catch(SocketTimeoutException e){
           e.printStackTrace();
          }catch(IOException e){
          e.printStackTrace();
          }