2011-09-02 90 views
0

我是Android新手。 我正在從ListView中下載Internet中的圖像。我在文件對象中獲取url,但是當我將它發送到Bitmap對象時,位圖對象返回null意味着圖像不會加載到位圖object.please回覆我。代碼在這裏:如何從文件中獲取url到位圖對象中

private Bitmap getBitmap(String url) { 
    String filename = String.valueOf(url.hashCode()); 

    File f = new File(cacheDir, filename); 
// here in f i getting image url 

    // here in bitmap the url is not loaded & get null 
    Bitmap bitmap = BitmapFactory.decodeFile(f.getPath()); 
    if(bitmap != null) return bitmap; 

    // Nope, have to download it 
    try { 
    bitmap = 
    BitmapFactory.decodeStream(new URL(url).openConnection().getInputStream()); 
    // save bitmap to cache for later 
    writeFile(bitmap, f); 

    return bitmap; 
    } catch (Exception ex) { 
    ex.printStackTrace(); 
    return null; 
    } 
} 

private void writeFile(Bitmap bmp, File f) { 
    FileOutputStream out = null; 

    try { 
    out = new FileOutputStream(f); 
    bmp.compress(Bitmap.CompressFormat.PNG, 80, out); 
    } catch (Exception e) { 
    e.printStackTrace(); 
    } 
    finally { 
    try { if (out != null) out.close(); } 
    catch(Exception ex) {} 
    } 
} 
+0

你想下載一個html頁面保存爲PNG圖片?或者URL指向一個png已經? – nurxyz

回答

2

我不認爲你正在正確地下載位圖。

CODE

這是我創建了一個會從你的網址,它會返回一個繪製的功能! 它將它保存到一個文件,並獲得它,如果它存在

如果不是,它會下載它並返回drawable。 您可以輕鬆編輯它,以將文件保存到您的文件夾。

/** 
    * Pass in an image url to get a drawable object 
    * 
    * @return a drawable object 
    */ 
    private static Drawable getDrawableFromUrl(final String url) { 
     String filename = url; 
     filename = filename.replace("/", "+"); 
     filename = filename.replace(":", "+"); 
     filename = filename.replace("~", "s"); 
     final File file = new File(Environment.getExternalStorageDirectory() 
       + File.separator + filename); 
     boolean exists = file.exists(); 
     if (!exists) { 
      try { 
       URL myFileUrl = new URL(url); 
       HttpURLConnection conn = (HttpURLConnection) myFileUrl 
         .openConnection(); 
       conn.setDoInput(true); 
       conn.connect(); 
       InputStream is = conn.getInputStream(); 
       final Bitmap result = BitmapFactory.decodeStream(is); 
       is.close(); 
       new Thread() { 
        public void run() { 
         ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
         result.compress(Bitmap.CompressFormat.JPEG, 40, bytes); 
         try { 
          if (file.createNewFile()){ 
           // 
          } 
          else{ 
           // 
          } 

          FileOutputStream fo; 
          fo = new FileOutputStream(file); 
          fo.write(bytes.toByteArray()); 
          fo.flush(); 
          fo.close(); 
         } catch (IOException e) { 
          e.printStackTrace(); 
         } 
        } 
       }.start(); 
       BitmapDrawable returnResult = new BitmapDrawable(result); 
       return returnResult; 
      } catch (FileNotFoundException e) { 
       e.printStackTrace(); 
      } catch (MalformedURLException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      return null; 
     } 
     else { 
      return new BitmapDrawable(BitmapFactory.decodeFile(file.toString())); 
     } 
    } 
0

我能想到的在這裏唯一的事情就是你缺少INTERNET權限在您的清單。

嘗試在AndroidManifest.xml中添加<uses-permission android:name="android.permission.INTERNET" />如果它現在還沒有