0

I don't think it's duplicating 'cause all the answers I've found were made several years ago. So, I load bitmap from URL and then do it this way:

currentView.setBackground(new BitmapDrawable(result));

...where "result" is Bitmap.

But "BitmapDrawable" is deprecated and it doesn't work from API 22 or 21. Are there some other way of converting Bitmap to Drawable or loading drawable from URL instead of Bitmap?

Justin McGuire
  • 365
  • 1
  • 5
  • 18
  • Why does it matter if the answers are from several years ago? – Pztar Jul 03 '15 at 23:09
  • You can use `Picasso` to do this. Here: http://stackoverflow.com/questions/26207716/add-background-image-to-android-listview-using-picasso – Damian Kozlak Jul 03 '15 at 23:10
  • possible duplicate of [how to get Image from url in android](http://stackoverflow.com/questions/6407324/how-to-get-image-from-url-in-android) – Pztar Jul 03 '15 at 23:10
  • @Pztar it does 'cause some methods weren't deprecated that time and are so now. – Justin McGuire Jul 03 '15 at 23:27

2 Answers2

0

As the documentation tells you - don't use the deprecated

new BitmapDrawable(result)

Instead use the API 18 version

new BitmapDrawable(getActivity().getResources(), result)

Assuming you're supporting API 18+, otherwise you need to handle both version depending on the runtime API.

Malthan
  • 7,005
  • 3
  • 20
  • 22
  • I tried, it's not deprecated anymore but layout background picture on my Nexus 5 emulator is still invisible (background is gray instead despite default background color was black). so what can be the reason in this case? – Justin McGuire Jul 03 '15 at 23:37
  • Are you sure the bitmap has been downloaded and processed correctly? You can check the preview of the bitmap with the Android Studio debugger. – Malthan Jul 03 '15 at 23:39
  • It's okay on my Samsung with 4.2.2. The problems are on only the highest APIs. (actually, some problems are on all the devices I run on, but that's another topic maybe :) ) – Justin McGuire Jul 03 '15 at 23:48
0

Use Glide to load image from URL, Glide is a fast and efficient open source media management and image loading framework for Android that wraps media decoding, memory and disk caching, and resource pooling into a simple and easy to use interface.

Glide takes care of making scrolling any list of images as smooth as possible and also receive, resize and display remote image.

For more information refer here

Add below dependencies in build.gradle:

dependencies {
    compile 'com.github.bumptech.glide:glide:3.6.0'
    compile 'com.android.support:support-v4:19.1.0'
}

Eg project https://github.com/chrisbanes/cheesesquare

Eg usage:

@Override
public void onCreate(Bundle savedInstanceState) {
    ...

    ImageView imageView = (ImageView) findViewById(R.id.my_image_view);

    Glide.with(this).load("http://goo.gl/gEgYUd").into(imageView);
}

In the load method just pass the URL of the image and in into just pass the view, in you case currentview. For your scenario:

Glide.with(this).load("http://goo.gl/gEgYUd").into(currentView);
Psypher
  • 10,717
  • 12
  • 59
  • 83