图片浏览问题 [英] Image View problem

查看:198
本文介绍了图片浏览问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ImageView img;
TextView tv;
Parser p= new Parser();

@Override
public void onCreate(Bundle savedInstanceState) {        
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    tv = (TextView) findViewById(R.id.txt);
    img = (ImageView) findViewById(R.id.cover);


    new AsyncTask<Void, Double, Void>() {

        @Override
        protected Void doInBackground(Void... params) {

            while (true) {
                publishProgress(Math.random());
                SystemClock.sleep(3000);

            }
        }

        @Override
        protected void onProgressUpdate(Double... values) {


            p.myHandler();
            img.setImageBitmap(p.bitmap);
            tv.setText("Artist : " + p.artist + "\n" + 
                       "Album : " + p.album + "\n" + 
                       "Song : " + p.title + "\n");
        }
    }.execute();
}

和也

p.bitmap =  BitmapFactory.decodeStream((InputStream)new URL(image).getContent());

但图像并不总是显示。图像出现和消失随机你能帮助我吗?

but the image doesn't always show. The image appears and disappears randomly can you help me please?

推荐答案

据的此链接,没有在 BitmapFactory.de codeStream 的错误。它的存在,至少在Android 2.1的SDK。

According to this link, there is a bug in the previous versions of BitmapFactory.decodeStream. It exists at least at the Android 2.1 SDK.

本类解决了这个问题:

class FlushedInputStream extends FilterInputStream {
    public FlushedInputStream(InputStream inputStream) {
        super(inputStream);
    }

    @Override
    public long skip(long n) throws IOException {
        long totalBytesSkipped = 0L;
        while (totalBytesSkipped < n) {
            long bytesSkipped = in.skip(n - totalBytesSkipped);
            if (bytesSkipped == 0L) {
                  int ibyte = read();
                  if (ibyte < 0) {
                      break;  // we reached EOF
                  } else {
                      bytesSkipped = 1; // we read one byte
                  }
           }
            totalBytesSkipped += bytesSkipped;
        }
        return totalBytesSkipped;
    }
}

和图像应如此下载:

//I'm not sure whether this line works, but just in case I use another approach
//InputStream is = (InputStream)new URL(image).getContent()
DefaultHttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(imageUrl);
HttpResponse response = client.execute(request);
InputStream is = response.getEntity().getContent();
//Use another stream
FlushedInputStream fis = new FlushedInputStream(is);
bmp = BitmapFactory.decodeStream(fis);

这篇关于图片浏览问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆