How to check android drawable type? - android

Can I check the type of drawable at runtime whether it is an xml shape/selector/layer-list or jpg/png/gif file?

You can get type of drawable with this:
public static String getTypeOfDrawable(int drawableId,Context context) {
Drawable resImg = context.getResources().getDrawable(drawableId);
return resImg.getClass().toString().replace("class android.graphics.drawable.","");
}
You will get result like:
BitmapDrawable
StateListDrawable
Then you if it is BitmapDrawable its not that hard to get format of file

Related

Using Bypass to parse Markdown in Android, not working with Images

I'm using bypass lib to parse a markdown string to show inside a TextView.
What basically this lib does is to parse all strings and build an SpannedString to show this inside a TextView. I've debuged all the lib code and aren't able to find what is doing wrong in a reasonable time. Is there someone that have been faced the same problem and can help me?
String markdownStr = "# Testing Markdown\n" +
"\n" +
"![surf](http://www.adesl.pt/images/outras-provas/surf.jpg)";
TextView markdownTxtView = (TextView) findViewById(R.id.markdown);
Bypass bypass = new Bypass(getApplicationContext());
CharSequence charSequence = bypass.markdownToSpannable(markdownStr, new Bypass.ImageGetter() {
#Override
public Drawable getDrawable(String source) {
//TODO: get drawable from source
Drawable drawable = getApplicationContext().getResources().getDrawable(R.drawable.test);
return drawable;
}
});
markdownTxtView.setText(charSequence);
by adding bellow code it works:
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
Picasso Image Getter
https://github.com/Commit451/BypassPicassoImageGetter
Glide Image Getter
https://github.com/victorlaerte/BypassGlideImageGetter

how do I transform a string of url's into drawable?

I currently have an arraylist as follows:
private void loadImages() {
images = new ArrayList<>();
images.add(getResources().getDrawable(R.drawable.imag1));
images.add(getResources().getDrawable(R.drawable.imag2));
images.add(getResources().getDrawable(R.drawable.imag3));
}
I want to be able to convert a url into these drawables such that:
drawable1 = "http.someimage.com/image.png"
drawable2 = "http.someimage.com/newimage.png"
followed by
private void loadImages() {
images = new ArrayList<>();
images.add(getResources().getDrawable(drawable1));
images.add(getResources().getDrawable(drawable2));
...etc }
Is there any easy way to go around this? I definitely want to stick to drawables ,but I cant find any way to convert a url to drawable
Any ideas? Thanks!
If you have a URL of the picture you need to download it first.
You can't "convert" a URL into a drawable.
you need something like this:
URL url = new URL("http.someimage.com/image.png");
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
Then if you need to add the image into an ImageView object you can call the method .setImageBitmap(bmp).
Otherwise there are ways to extract a Drawable object from the Bitmap
you can check this previous answer. then once you have the drawable you can add it to your arraylist.
Hope I got your question right
P.S.: be sure not to do this on main thread since it is a network operation! use a thread or an asynctask

Assert ImageView was loaded with specific drawable resource ID

I'm writing a Robolectric unit test and I need to make an assertion that an ImageView had setImageResource(int) called on it with a certain resource ID. I'm using fest-android for assertions but it doesn't appear to contain this assertion.
I also tried to get the ShadowImageView from Robolectric for the ImageView because I know it used to give you access to this, but it's now gone.
Lastly, I tried to call setImageDrawable in my code instead of setImageResource, then in my test assert like this:
assertThat(imageView).hasDrawable(resources.getDrawable(R.drawable.some_drawable));
but this also fails, even though the failure message clearly shows it's the same Drawable being loaded.
For Background
ImageView imageView = (ImageView) activity.findViewById(R.id.imageview);
assertEquals(R.drawable.expected, Robolectric.shadowOf(imageView.getBackground()).getCreatedFromResId());
For Drawable
ImageView imageView = (ImageView) activity.findViewById(R.id.imageview);
assertEquals(R.drawable.expected, Robolectric.shadowOf(imageView.getDrawable()).getCreatedFromResId());
From Roboelectric 3.0+
This is how you can do:
int drawableResId = Shadows.shadowOf(errorImageView.getDrawable()).getCreatedFromResId();
assertThat("error image drawable", R.drawable.ic_sentiment_dissatisfied_white_144dp, is(equalTo(drawableResId)));
I ended up extending fest-android to solve this:
public class CustomImageViewAssert extends ImageViewAssert {
protected CustomImageViewAssert(ImageView actual) {
super(actual);
}
public CustomImageViewAssert hasDrawableWithId(int resId) {
boolean hasDrawable = hasDrawableResourceId(actual.getDrawable(), resId);
String errorMessage = String.format("Expected ImageView to have drawable with id <%d>", resId);
Assertions.assertThat(hasDrawable).overridingErrorMessage(errorMessage).isTrue();
return this;
}
private static boolean hasDrawableResourceId(Drawable drawable, int expectedResId) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
Bitmap bitmap = bitmapDrawable.getBitmap();
ShadowBitmap shadowBitmap = (ShadowBitmap) shadowOf(bitmap);
int loadedFromResourceId = shadowBitmap.getCreatedFromResId();
return expectedResId == loadedFromResourceId;
}
}
The magic sauce is:
ShadowBitmap shadowBitmap = (ShadowBitmap) shadowOf(bitmap);
int loadedFromResourceId = shadowBitmap.getCreatedFromResId();
which is Robolectric specific, so I can't submit a pull request to fest-android with this.
With kotlin extensions:
fun ImageView.hasDrawable(resId: Int): Boolean =
Shadows.shadowOf(this.drawable).createdFromResId == resId
fun ImageView.hasBackground(resId: Int): Boolean =
Shadows.shadowOf(this.background).createdFromResId == resId
Usage:
assertTrue(image.hasDrawable(R.drawable.image_resource))

create drawable from path problem

For some reason d=null when creating a drawable from coverArtURLStr, which is the full http path to the resource on the LAN.
Anything obvious wrong? It's a path to a .png
[I can access the LAN OK, and Data.defaultCoverArt works fine]
public static void updateCoverArt(String coverArtURLStr)
{
String coverArtURL = coverArtURLStr;
Drawable d;
if (coverArtURL.equals(""))
d = Data.defaultCoverArt;
else
d = Drawable.createFromPath(coverArtURL);
Data.coverArtIV.setImageDrawable(d);
}
I don't think Drawable.createFromPath() method can handle URLs. Try this solution Android Drawable Images from URL.

How to convert an image from URL to Drawable

I'm having a problem converting an image to a Drawable object. I'm converting the image using:
public Drawable LoadImageFromWebOperations(String url) {
try {
InputStream is = (InputStream)new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
} catch (Exception e) {
System.out.println("Exc=" + e);
return null;
}
}
I'm trying to put the Drawable in a HashMap and insert that into a ListAdapter, however the value of the Drawable always is something like android.graphics.drawable.BitmapDrawable#405359b0 instead of an integer and I get the message in logcat
resolveUri failed on bad bitmap uri".
this is how I put the Drawable in the HashMap:
map.put("cover", String.valueOf(
Main.this.LoadImageFromWebOperations("http://www.asdfasfs.com/dasfas.jpg")));
Why do you expect the drawable to be an integer? It is an object you can assign to an imageView.
There are items in your project you can refer to with their ID, that is true, but that is something else.
R.drawable.icon is not a Drawable, in the same sense that R.view.your_Button is not a Button. You would call something like getViewFromId() on that. If you have a function that works like this:
doSomethingWithView(R.view.id);
Then it would not work with (pseudocode ofcourse)
myView = new Button();
doSomethingWithView(myView);
So if your function works with a R.drawable.id, it is highly unlikely that it works with a Drawable (except with overloading ofcourse).

Categories

Resources