Reputation: 5414
UPDATE:
After searching a bit I don't seem to be alone about this problem:
http://code.google.com/p/android/issues/detail?id=6066
https://groups.google.com/forum/?fromgroups#!topic/android-beginners/dDnHEacrpCE
There's two solutions ATM:
1. (Only works in some cases) Before calling the static decodeStream() method, implement a Thread.sleep(300) (Maybe the sleep duration value have to be higher, but 300 ms works for me)
2. Replace the following:
URL pictureurl = new URL("http://www.somewebsite.com/picture15.jpg");
URLConnection urlConn = pictureurl.openConnection();
urlConn.connect();
InputStream urlStream = urlConn.getInputStream();
with the following (As suggested by imran khan):
HttpGet httpRequest = new HttpGet();
httpRequest.setURI(new URI("http://www.somewebsite.com/picture15.jpg"));
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
HttpEntity entity = response.getEntity();
BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
Bitmap bm = BitmapFactory.decodeStream(bufHttpEntity.getContent());
httpRequest.abort();
I therefore accept imran khan's answer, as it's the only answer of all the answers in this post that's working.
I have the following code in my Android app:
URL pictureurl = new URL("http://www.somewebsite.com/picture15.jpg");
URLConnection urlConn = pictureurl.openConnection(); // NOT NULL
urlConn.setRequestProperty("Referer", "http://www.somewebsite.com/");
urlConn.connect();
InputStream urlStream = urlConn.getInputStream(); // NOT NULL
Bitmap bm = BitmapFactory.decodeStream(urlStream);
Bitmap bm2 = Bitmap.createScaledBitmap(bm, 100, 100, true);
imageView.setImageBitmap(bm2);
The method "decodeStream()" returns null, when I use a certain image from a certain website but when I load the image in my browser it shows up just fine. I can use other images from other websites that result in the method "decodeStream()" returning the expected Bitmap instance.
I noticed the following text in the method explanation of the "decodeStream()" method:
"If the input stream is null, or cannot be used to decode a bitmap, the function returns null" - My input stream is NOT null!
Following picture results in the method "decodeStream()" returning null:
http://i45.tinypic.com/eah2d2.jpg
Following picture results in the method "decodeStream()" returning the expected bitmap instance:
I am using Android 1.5.
Is this a bug in the Android/Java environment or am I doing something wrong?
Upvotes: 0
Views: 1297
Reputation: 3521
You need to pass BufferedInputStream.
BufferedInputStream bis = new BufferedInputStream( is );
bmp = BitmapFactory.decodeStream( bis );
so your code to decode stream will be.
InputStream urlStream = urlConn.getInputStream(); // NOT NULL
BufferedInputStream bis = new BufferedInputStream( urlStream );
Bitmap bm = BitmapFactory.decodeStream(bis);
Bitmap bm2 = Bitmap.createScaledBitmap(bm, 100, 100, true);
Upvotes: 0
Reputation: 132982
try following:
HttpGet httpRequest = new HttpGet();
httpRequest.setURI(new URI("http://www.somewebsite.com/picture15.jpg"));
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
HttpEntity entity = response.getEntity();
BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
Bitmap bm = BitmapFactory.decodeStream(bufHttpEntity.getContent());
httpRequest.abort();
Upvotes: 3