Bitmap From Url Android
I'm trying to get a png Bitmap from URL but the Bitmap is always NULL in with this code: private class DownloadImageTask extends AsyncTask { Ima
Solution 1:
This is a simple one line way to do it:
URLurl=newURL("http://....");
Bitmapimage= BitmapFactory.decodeStream(url.openConnection().getInputStream());
Solution 2:
I suggest to use a Thead (async) to avoid exception errors :
Threadthread=newThread(newRunnable(){
@Overridepublicvoidrun() {
Bitmap thumb;
// Ur URLStringlink= value.toString();
try {
URLurl=newURL(link);
thumb = BitmapFactory.decodeStream(url.openConnection().getInputStream());
// UI component
imageView.setImageBitmap(thumb);
} catch (Exception e) {
Log.e("error message", Objects.requireNonNull(e.getMessage()));
}
}
});
thread.start();
Solution 3:
You can try code below...
publicstatic Bitmap loadBitmap(String url) {
Bitmapbitmap=null;
InputStreamin=null;
BufferedOutputStreamout=null;
intIO_BUFFER_SIZE=4 * 1024;
try {
URIuri=newURI(url);
url = uri.toASCIIString();
in = newBufferedInputStream(newURL(url).openStream(),
IO_BUFFER_SIZE);
finalByteArrayOutputStreamdataStream=newByteArrayOutputStream();
out = newBufferedOutputStream(dataStream, IO_BUFFER_SIZE);
int bytesRead;
byte[] buffer = newbyte[IO_BUFFER_SIZE];
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
out.flush();
finalbyte[] data = dataStream.toByteArray();
BitmapFactory.Optionsoptions=newBitmapFactory.Options();
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,
options);
} catch (IOException e) {
returnnull;
} catch (URISyntaxException e) {
e.printStackTrace();
} finally {
try {
in.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return bitmap;
}
Solution 4:
If you try the http url in a browser you see that it redirects to a https. Thats your problem. BitmapFactory.decodeStream will not do this redirection so it returns null.
Solution 5:
To get a bitmap from URL, try this:
public Bitmap getBitmapFromURL(String src) {
try {
java.net.URLurl=newjava.net.URL(src);
HttpURLConnectionconnection= (HttpURLConnection) url
.openConnection();
connection.setDoInput(true);
connection.connect();
InputStreaminput= connection.getInputStream();
BitmapmyBitmap= BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
returnnull;
}
}
Or:
publicstatic Bitmap loadBitmap(String url) {
Bitmapbitmap=null;
InputStreamin=null;
BufferedOutputStreamout=null;
try {
in = newBufferedInputStream(newURL(url).openStream(), IO_BUFFER_SIZE);
finalByteArrayOutputStreamdataStream=newByteArrayOutputStream();
out = newBufferedOutputStream(dataStream, IO_BUFFER_SIZE);
copy(in, out);
out.flush();
finalbyte[] data = dataStream.toByteArray();
BitmapFactory.Optionsoptions=newBitmapFactory.Options();
//options.inSampleSize = 1;
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,options);
} catch (IOException e) {
Log.e(TAG, "Could not load Bitmap from: " + url);
} finally {
closeStream(in);
closeStream(out);
}
return bitmap;
}
Post a Comment for "Bitmap From Url Android"