下载图像表单Web时,BitmapFactory.decodeStream返回null

时间:2012-12-06 11:18:05

标签: android bitmap bitmapfactory

我正在尝试使用Google Example Page从网址下载图片。当我在BitmapFactory.decodeStream方法中使用InputStream时,我read,我不能使用两次。我试图这样做,但它不起作用'因为它在解码图像中返回null,我不知道我能做什么。

这是我的代码:

这部分是AsyncTask类中的doInBackground方法

Bitmap bitmapImage;
URL imageUrl = null;
try {
imageUrl = new URL(url[0]);

HttpGet httpRequest = null;
httpRequest = new HttpGet(imageUrl.toURI());

HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);

HttpEntity entity = response.getEntity();
BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
InputStream instream = bufHttpEntity.getContent();

    bitmapImage = CommonMethods.decodeSampledBitmapFromResource(instream, thumb_width, thumb_width);

instream.close();
return bitmapImage;

 } catch (URISyntaxException e) {
    e.printStackTrace();
    return null;
 } catch (MalformedURLException e) {
    e.printStackTrace();
    return null;
 } catch (IOException e) {
    e.printStackTrace();
    return null;
 }


 public static Bitmap decodeSampledBitmapFromResource(InputStream instream,
        int reqWidth, int reqHeight) throws IOException {

    //Copy instream for decode twice 
ByteArrayOutputStream out = new ByteArrayOutputStream();
    copy(instream,out);
    ByteArrayInputStream instream2 = new ByteArrayInputStream(out.toByteArray());

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(instream, null, options);
    instream2.close();

    options.inJustDecodeBounds = false;

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    return BitmapFactory.decodeStream(instream, null, options);
 }

 public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {

     // Raw height and width of image
     final int height = options.outHeight;
     final int width = options.outWidth;
     int inSampleSize = 1;

     if (height > reqHeight || width > reqWidth) {
         if (width > height) {
         inSampleSize = Math.round((float) height / (float) reqHeight);
     } else {
             inSampleSize = Math.round((float) width / (float) reqWidth);
     }
     }

     return inSampleSize;
}

//Copy instream method
public static void copy(InputStream input, OutputStream output) throws IOException{

     byte[] buffer = new byte[Constants.IO_BUFFER_SIZE];

 int n = 0;

 while (-1 != (n = input.read(buffer))) {

     output.write(buffer, 0, n);
 }
 }

5 个答案:

答案 0 :(得分:5)

BitmapFactory.decodeStream返回null,因为输入流被使用了两次,我没有尝试过你的代码,但它接缝正常,或者我错了。 无论如何,我有一个更好的解决方案。只需使用BufferedInputStream包装inputStream,在第二次读取之前,调用" reset"第一。注意普通的inputStreams不支持" reset",你可以调用它但不会发生任何事情。 我的代码:

    public static Bitmap decodeSampledBitmapFromStream(InputStream inputStream,
                                                   int reqWidth, int reqHeight)
                             throws IOException {
    if (!widthHeightCheck(reqWidth, reqHeight)) 
        return BitmapFactory.decodeStream(inputStream);
    // First decode with inJustDecodeBounds=true to check dimensions
    if (!(inputStream instanceof BufferedInputStream)) {
        inputStream = new BufferedInputStream(inputStream);
    }
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    Rect rect = new Rect(-1, -1, -1, -1);
    BitmapFactory.decodeStream(inputStream, rect, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    inputStream.reset();
    return BitmapFactory.decodeStream(inputStream, rect, options);
}

答案 1 :(得分:1)

我认为您可以通过在自定义WrappedStream中包装从httpEntity获取的流来实现此目的。此WrappedStream将在读取原始流时提供第二个输入流。 (这是用PipedStream完成的)

使用此代码获取图像大小后:

options.inJustDecodeBounds = true;
WrappedStream wrappedStream = new WrappedStream(instream);
BitmapFactory.decodeStream(wrappedStream, null, options);

你可以打电话

InputStream reReadStream = wrappedStream.getReReadStream();
options.inJustDecodeBounds = false;
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

// Decode bitmap with inSampleSize set
return BitmapFactory.decodeStream(reReadStream, null, options);

最后,这里是WrappedStream的实现(它只是将所有调用委托给包装的inputStream,并在pipedOutputStream中写入所有读取(或跳过)的字节)

import java.io.IOException;
import java.io.InputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

/** Simple class wrapping an InputStream and feeding a secondary InputStream
 *  to re-read the data that was originally available in the inputStream.
**/

public class WrappedStream extends InputStream {

private InputStream urlStream;
private PipedOutputStream pipedStream;

public WrappedStream(InputStream urlStream) {
    this.urlStream = urlStream;
    this.pipedStream = new PipedOutputStream();
}

/**
 * return a fresh InputStream to re-read the data
 */
public InputStream getReReadStream() throws IOException {
    return new PipedInputStream(pipedStream);
}
@Override
public int available() throws IOException {
    return urlStream.available();
}

@Override
public void close() throws IOException {
    urlStream.close();
}

@Override
public void mark(int readlimit) {
    urlStream.mark(readlimit);
}

@Override
public boolean markSupported() {
    return urlStream.markSupported();
}

@Override
public int read() throws IOException {
    int b = urlStream.read();
    pipedStream.write(b);
    return b;
}

@Override
public int read(byte[] buffer) throws IOException {
    int l = urlStream.read(buffer);
    pipedStream.write(buffer);
    return l;
}

@Override
public int read(byte[] buffer, int offset, int length) throws IOException {
    int l = urlStream.read(buffer, offset, length);
    pipedStream.write(buffer, offset, length);
    return l;
}

@Override
public void reset() throws IOException {
    urlStream.reset();
}

@Override
//bytes skipped must available on the re-read stream so we read and write them.
public long skip(long byteCount) throws IOException {
    long bytesToSkip = byteCount;
    long skippedBytes = 0;
//ugly trick required to not loosing bytes if we ever skip more than Integer.MAX_VALUE bytes
    while(bytesToSkip>Integer.MAX_VALUE){
        _skip(Integer.MAX_VALUE);
        bytesToSkip -=Integer.MAX_VALUE;
        skippedBytes +=Integer.MAX_VALUE;
    }
    byte[] b = new byte[(int)bytesToSkip];
    skippedBytes += read(b);
    return skippedBytes;
}

private int _skip(int byteCount) throws IOException {
    byte[] b = new byte[(int)byteCount];
    return read(b);
}
}

请注意,我没有测试此代码。这只是为了给你一些如何解决问题的想法。

另一点:即使此代码永远不会创建一个巨大的位图,整个流将保留在内存中,直到构建缩放的位图。

答案 2 :(得分:0)

这是从服务器下载位图的方法,只需更少的代码即可满足您的要求

Bitmap downloadBitmap(String url)
        {
            Bitmap image = null;
            InputStream in = null;
            try
                {
                    in = new java.net.URL(url).openStream();
                    BitmapFactory.Options opts = new BitmapFactory.Options();
                    opts.inSampleSize = 2;
                    image = BitmapFactory.decodeStream(new FlushedInputStream(in),null,opts);
                    in.close();
                }
            catch (MalformedURLException e)
                {
                    e.printStackTrace();
                }
            catch (IOException e)
                {
                    e.printStackTrace();
                }
            return image;
        }

在上面的代码中我们使用     opts.inSampleSize = 2; 这意味着位图将缩小到原始大小的一半,以避免内存异常,如果我们要加载大量图像,我们必须这样做

其中使用的其他一些课程

        static 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 byte1 = read();
                                    if (byte1 < 0)
                                        {
                                            break; // we reached EOF
                                        }
                                    else
                                        {
                                            bytesSkipped = 1; // we read one byte
                                        }
                                }
                            totalBytesSkipped += bytesSkipped;
                        }
                    return totalBytesSkipped;
                }
        }

答案 3 :(得分:0)

找到适合你的代码

final HttpEntity entity = response.getEntity();
            if (entity != null) {
                InputStream inputStream = null;
                try {
                    inputStream = entity.getContent();
                    BitmapFactory.Options options = new BitmapFactory.Options();
                    //options.inSampleSize = 2;
                    final Bitmap bitmap = BitmapFactory
                            .decodeStream(inputStream, null, options);
                    return bitmap;
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (inputStream != null) {
                        inputStream.close();
                    }
                    entity.consumeContent();
                }
            }

请替换正确的变量,如果您想缩放图像,可以在获取位图后缩放图像。

答案 4 :(得分:0)

请使用以下代码下载并将图像显示到imageview。

public class image extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Bitmap bitmap = DownloadImage("http://www.gophoto.it/view.php?i=http://1.bp.blogspot.com/-2LTvCCufBKc/T3L3KgcTj2I/AAAAAAAABbQ/Ki60e1LU9sE/s1600/Sachin%2BTendulkar.png");
        ImageView img = (ImageView) findViewById(R.id.img);
        img.setImageBitmap(bitmap);
    }

    private InputStream OpenHttpConnection(String urlString) throws IOException {
        InputStream in = null;
        int response = -1;

        URL url = new URL(urlString);
        URLConnection conn = url.openConnection();

        if (!(conn instanceof HttpURLConnection))
            throw new IOException("Not an HTTP connection");

        try {
            HttpURLConnection httpConn = (HttpURLConnection) conn;
            httpConn.setAllowUserInteraction(false);
            httpConn.setInstanceFollowRedirects(true);
            httpConn.setRequestMethod("GET");
            httpConn.connect();
            response = httpConn.getResponseCode();
            if (response == HttpURLConnection.HTTP_OK) {
                in = httpConn.getInputStream();
            }
        } catch (Exception ex) {
            throw new IOException("Error connecting");
        }
        return in;
    }

    private Bitmap DownloadImage(String URL) {
        Bitmap bitmap = null;
        InputStream in = null;
        try {
            in = OpenHttpConnection(URL);
            bitmap = BitmapFactory.decodeStream(in);
            in.close();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        return bitmap;
    }
}