2011-09-02 86 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" />如果它现在还没有