代码之家  ›  专栏  ›  技术社区  ›  Romain Piel

BitmapFactory.decodeStream文件(inputStream)当某些字节出错时总是返回null

  •  3
  • Romain Piel  · 技术社区  · 16 年前

    我正在构建一个android应用程序,目前从URL检索位图时遇到问题。下面是我使用的代码:

    public static Bitmap bmpFromURL(URL imageURL){
    
        Bitmap result = null;
    
        try {
    
            HttpURLConnection connection = (HttpURLConnection)imageURL .openConnection();
    
            connection.setDoInput(true);
    
            connection.connect();
    
            InputStream input = connection.getInputStream();
    
            result = BitmapFactory.decodeStream(input);
    
    
        } catch (IOException e) {
    
    
            e.printStackTrace();
    
        }
    
        return result;
    
    }
    

    BitmapFactory.decodeStream :

    如果输入流为null,或者不能用于解码位图,则函数返回null。流的位置将是在编码数据被读取后的位置。

    有没有办法忽略那些错误的像素?可能是选项参数?

    罗曼

    2 回复  |  直到 16 年前
        1
  •  5
  •   Romain Guy    16 年前

        2
  •  1
  •   Timuçin    14 年前

    试试这个…对我有用。希望有帮助。

    result = BitmapFactory.decodeStream(new PatchInputStream(input));
    
    public class PatchInputStream extends FilterInputStream {
       public PatchInputStream(InputStream input) {
           super(input);
       }
       public long skip(long n) throws IOException {
           long m = 0L;
           while (m < n) {
               long _m = in.skip(n-m);
               if (_m == 0L) break;
               m += _m;
           }
           return m;
       }
    }
    
    推荐文章