2011-09-06 135 views
0

我下载很多图像形成服务器,一些图像显示良好,但其他没有显示:我的关键代码:下载PIC,但PIC不会显示

private Bitmap getBitmap(String url) 
{ 
    //I identify images by hashcode. Not a perfect solution, good for the demo. 
    String filename=String.valueOf(url.hashCode()); 
    File f=new File(cacheDir, filename); 

    //from SD cache 
    Bitmap b = decodeFile(f); 
    if(b!=null) 
     return b; 

    //from web 
    try { 
     Bitmap bitmap=null; 
     InputStream is=new URL(url).openStream(); 
     OutputStream os = new FileOutputStream(f); 
     Utils.CopyStream(is, os); 
     os.close(); 
     bitmap = decodeFile(f); 
     return bitmap; 
    } catch (Exception ex){ 
     ex.printStackTrace(); 
     return null; 
    } 
} 

//decodes image and scales it to reduce memory consumption 
private Bitmap decodeFile(File f){ 
    try { 
     //decode image size 
     BitmapFactory.Options o = new BitmapFactory.Options(); 
     o.inJustDecodeBounds = true; 
     BitmapFactory.decodeStream(new FileInputStream(f),null,o); 

     //Find the correct scale value. It should be the power of 2. 
     final int REQUIRED_SIZE=70; 
     int width_tmp=o.outWidth, height_tmp=o.outHeight; 
     int scale=1; 
     while(true){ 
      if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE) 
       break; 
      width_tmp/=2; 
      height_tmp/=2; 
      scale++; 
     } 

     //decode with inSampleSize 
     BitmapFactory.Options o2 = new BitmapFactory.Options(); 
     o2.inSampleSize=scale; 
     return BitmapFactory.decodeStream(new FileInputStream(f), null, o2); 
    } catch (FileNotFoundException e) {} 
    return null; 
} 


    public class Utils { 
public static void CopyStream(InputStream is, OutputStream os) 
{ 

    final int buffer_size=1024; 
    try 
    { 
     byte[] bytes=new byte[buffer_size]; 
     for(;;) 
     { 
      int count=is.read(bytes, 0, buffer_size); 
      if(count==-1) 
       break; 
      os.write(bytes, 0, count); 
     } 
    } 
    catch(Exception ex){} 
} 

我也看了http://code.google.com/p/android/issues/detail?id=6066,在这些链接它使用

Bitmap bmp = BitmapFactory.decodeStream(new PatchInputStream(in)); 

,但在我的代码,我用

BitmapFactory.decodeStream(new FileInputStream(f),null,o); 

我DONOT知道如何形成的FileInputStream(六)变更为p atchInputStream(in),你能给我一些建议吗?谢谢

+0

什么是patchInputStream( ) 这里? –

+0

IH链接http://code.google.com/p/android/issues/detail?id=6066 – pengwang

+0

patchInputStream正在扩展FilterInputStream的类 更多细节该类找 http://download.oracle.com /javase/1.4.2/docs/api/java/io/FilterInputStream.html – Mihir

回答

1

尝试这种::

 tran_btn_skip = (ImageView) findViewById(R.id.tran_btn_skip); 
try { 
     Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(
       "http://xyz/MRESC/images/test/skip.png") 
       .getContent()); 
     tran_btn_skip.setImageBitmap(bitmap); 
    } catch (Exception e) { 
    } 

其中tran_btn_skipimageview或者你可以采取ImageButton

店面形象在SD卡::

save image to sdcard android Directory problem

+0

你使用非常复杂的方法,请尝试这个,并告诉我 –

+0

我知道你的方法是正确的,但我需要先保存图片到一个目录 – pengwang

+0

为什么你需要存储在SD卡? –