开发者

download pic,but the pic not show

i download many images form server,some images show well,but other not show: my key code :

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){}
}

i also read http://code.google.com/p/android/issues/detail?id=6066 ,in these link it used

 Bitmap bmp = BitmapFactory.decodeStre开发者_如何学Cam(new PatchInputStream(in));

but in my code i used

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

i donot know how to change form FileInputStream(f) to patchInputStream(in),can you give me some advice? thank you


try this ::

      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) {
    }

where tran_btn_skip is imageview or you can take ImageButton

store image in sdcard ::

save image to sdcard android Directory problem

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜