I have a question about loading an image from a website. The code I use is:
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
Bitmap bit=null;
try {
bit = BitmapFactory.decodeStream((InputStream)new URL("http://www.mac-wallpapers.com/bulkupload/wallpapers/Apple%20Wallpapers/apple-black-logo-wallpaper.jpg").getContent());
} catch (Exception e) {}
Bitmap sc = Bitmap.createScaledBitmap(bit,width,height,true);
canvas.drawBitmap(sc,0,0,null);
But it always returns a null pointer exception and the program crashes out. The URL is valid, and it seems to work for everyone else. I'm using 2.3.1.
Follow method to get url to bitmap in android just pass link of this image and get bitmap.
If you are using Picasso or Glide or Universal-Image-Loader for load image from url.
You can simply get the loaded bitmap by
For Picasso (current version
2.71828
)Java code
Kotlin code
For Glide
Check How does one use glide to download an image into a bitmap?
For Universal-Image-Loader
Java code
Please try this following steps.
1) Create AsyncTask in class or adapter(if you want to change the list item image).
2) Call the AsyncTask from your activity, fragment or adapter(inside onBindViewHolder).
2.a) For adapter:
2.b) For activity and fragment:
3) Kindly run the app and check the image.
Happy coding....:)
very fast way , this method works very quickly:
If you load URL from bitmap without using AsyncTask, write two lines after setContentView(R.layout.abc);