2011-05-11 64 views
0

在我的活動中,我從url下載圖片。我只想第一次下載這些圖像。稍後當我訪問此頁面時,它應該從sdcard中獲取圖像。我怎樣才能做到這一點?誰能幫忙?如何將下載的文件存儲到SD卡然後檢索它?

清單中我已設置權限:

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

,我使用的下載方法是:

public static Bitmap downloadFileFromUrl(String fileUrl){ 

     URL myFileUrl =null; 
     Bitmap imageBitmap = null; 

     try { 
      myFileUrl= new URL(fileUrl); 
     } 
     catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } 

     try { 
      HttpURLConnection connection= (HttpURLConnection)myFileUrl.openConnection(); 
      connection.setDoInput(true); 
      connection.connect(); 
      InputStream is = connection.getInputStream(); 
      imageBitmap = BitmapFactory.decodeStream(is); 

      //Below two lines I just tried out for saving to sd card. 
      FileOutputStream out = new FileOutputStream(fileUrl); 
      imageBitmap.compress(Bitmap.CompressFormat.PNG, 90, out); 
     } 
     catch (IOException e) { 
      e.printStackTrace(); 
     }catch (Exception e) { 
      e.printStackTrace(); 
     } 

     return imageBitmap; 
    } 

回答

0

你應該在一些地方保存你在高速緩存中已經得到了什麼。

或者如果你的文件名是唯一的,你必須檢查文件是否存在。

+0

u能用代碼解釋一下? – Mathew 2011-05-11 10:01:49

0

你必須寫數據從InputStream中指定SD卡的位置得到了..

2

嘗試此方法

public void DownloadImage(String imageUrl)  
    { 
     InputStream is = null; 
     if((imageUrl == null) || (imageUrl.length() == 0) || (imageUrl == " ")) 
     { 
      System.out.println("No need to download images now"); 
     } 
     else 
     { 
      System.gc(); 


      String[] items; 

      String ImageName = null; 

      URL myFileUrl =null; 
      Bitmap bmImg = null; 

      String path = IMAGE_DOWNLOAD_PATH; 

      FileOutputStream outStream = null; 
      File file = new File(path); 

      if(!file.exists()) 
      { 
       file.mkdirs(); 
      } 

      File outputFile; 
      BufferedOutputStream bos; 

       try { 
        myFileUrl= new URL(imageUrl.trim()); 

        HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection(); 
        conn.setRequestMethod("GET"); 

        conn.setDoInput(true); 
        conn.setConnectTimeout(20000); 
        conn.connect(); 

        is = conn.getInputStream(); 


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



       ImageName = getImageName(imageUrl); 

       try { 
        outputFile = new File(file, ImageName); 

        if(outputFile.exists())     
        { 
         System.out.println("No need to download image it already exist"); 
         outputFile.delete(); 
        } 
         outputFile.createNewFile(); 

         outStream = new FileOutputStream(outputFile); 
         //bos = new BufferedOutputStream(outStream); 
         BufferedInputStream bis = new BufferedInputStream(is); 

          ByteArrayBuffer baf = new ByteArrayBuffer(50); 

          int current = 0; 
          while ((current = bis.read()) != -1) 
          { 
           baf.append((byte) current); 
          } 

          outStream.write(baf.toByteArray()); 
          outStream.close(); 




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

    } 

,然後從SD卡獲取圖像,

File extStore = Environment.getExternalStorageDirectory(); 
     String file_path = "/(folder name)/"+"(image name)".trim()+".extension".trim(); 

     String mypath = extStore + file_path; 
     Bitmap bmp=BitmapFactory.decodeFile(mypath); 
ImageView image = (ImageView) v.findViewById(R.id.image); 
     image.setImageBitmap(bmp); 
相關問題