Android: Changing Wallpaper to Drawable - java

I'm using this code to change the wallpaper of the android home
WallpaperManager wm = WallpaperManager.getInstance(this);
wm.setBitmap(myBitmap);
I would like to set the background to a drawable. Is this possible?

You'll first have to convert the Drawable to a Bitmap. How to do this I found here. You'll need to use the BitmapFactory class, specifically the decodeResource() method.
Simply pass in the Resources and the Resource ID as parameters, like so:
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.my_drawable);
wm.setBitmap(bmp);

Related

How do I access an image as a Drawable in AndroidStudio?

I'm working on an android app using java in AndroidStudio. I have an ImageView pic and I want to set its image as a .png that I have saved in my /drawable folder. I may try
pic.setImageDrawable(R.drawable.image_name);
However, setImageDrawable() requires a Drawable, while R.drawable.image_name returns an Integer (the ID of the image).
How may I resolve this?
You can use setImageResource() for this:
pic.setImageResource(R.drawable.image);
More about it here
Use this for java
ContextCompat.getDrawable(getActivity(), R.drawable.drawable_resource_name);
and then set this drawable to your image view

How can I get the ID from an image from camera or internal storage to put into Android EasySlider?

I'm programming a new app for my city and I need help with some code. I'm using the EasySliders library for Android, and I need to put images taken from either the camera or internal storage.
I could put them in ImageViews, but I want to put them into a slider. EasySlider needs 2 parameters:
A title for the image
An ID
How can I get the id from those pictures?
Bitmap bitmap;
bitmap = BitmapFactory.decodeFile(dir);
// Here is the problem, the bitmap is not an id and it causes an error
easySliders.add(new SliderItem("temporalname", bitmap));

Using a String variable to dynamically add SVG to ImageView

Basically, I'm trying to add an SVG drawable to an ImageView, but I keep getting strange type errors I can't resolve.
// String var "icon" holds the name of the drawable resource
int drawableId = context.getResources()
.getIdentifier(icon, "drawable", MainActivity.PACKAGE_NAME);
// drawableId shows the error:
// 'getDrawable(android.content.Context, int)' in 'android.support.v4.content.ContextCompat' cannot be applied to '(int)'
// I tried using Context instead of ContextCompat, but I got an error about needing API 21
Drawable channelIcon = ContextCompat.getDrawable(drawableId);
// drawableId here shows no error
imageView.setImageResource(drawableId);
Any idea what I'm doing wrong? Basically this is a workaround for needing to use a variable for R.drawable.something
Thanks.
I'm trying to add an SVG drawable to an ImageView
ImageView does not support SVG.
Any idea what I'm doing wrong?
Beyond the aforementioned issue, replace:
Drawable channelIcon = ContextCompat.getDrawable(drawableId);
with:
Drawable channelIcon = ContextCompat.getDrawable(context, drawableId);

How to change background in Java?

I'm working on an app that allows changing theme. To achieve that, I need to change background in java.
I created imageView and tried to set background as imageView.
I was using this code:
ImageView imgViewBackground =(ImageView) findViewById(R.id.imageViewBackground);
int ID = getResources().getIdentifier("imagebackground", "drawable", getPackageName());
imgViewBackground.setImageResource(ID);
but the app crashes after 20-30 seconds of usage.
I also tried this, but the app crashes on startup:
RelativeLayout layout =(RelativeLayout)findViewById(R.id.imageViewBackground);
layout.setBackgroundResource(R.drawable.imagebackground);
Is there an effective way to change background directly, and not through imageView in java?
Why not simply do this:
ImageView imgViewBackground =(ImageView) findViewById(R.id.imageViewBackground);
imgViewBackground.setImageResource(R.drawable.imagebackground);
P.S: An image by the name imagebackground must be present in the drawable folder.

modify universal-image-loader

I am using this project universal-image-loader in order to display in a gridview several images. But I would like to modify these images with some html (for example add ads at the bottom of each images).
I saw the image are displayed by calling imageLoader.displayImage(..), in the method getView of the ImageAdapter.
I don't know what approach is better:
add a step in displayImage(...) in order to create a webview with my image and html and transform this webview in bitmap?
modify the getView of ImageAdapter which will create the webview and add an argument webview in displayeImage()?
...
...
I guess I don't have the choice, I have to use a webview.
Moreover in the method displayImage() of ImageLoader.java, i don't understand where the images are loaded, I guess this is somewhere in this line, but when I am looking in these methods I can't find/understand
ImageSize targetSize = getImageSizeScaleTo(imageView);
String memoryCacheKey = MemoryCacheUtil.generateKey(uri, targetSize);
cacheKeysForImageViews.put(imageView.hashCode(), memoryCacheKey);
Bitmap bmp = configuration.memoryCache.get(memoryCacheKey);
(How from a string built by an uri and ImageSize we can have a bitmap? where is the image?)
Images are loaded asynchronously. So your code piece is just a top of complex logic. Main work is done in LoadAndDisplayTask class but you don't need to touch it.
For your case you can create own BitmapDisplayer:
create BitmapDisplayer implementation
implement Bitmap display(Bitmap bitmap, ImageView imageView) method
in this method you can process incoming Bitmap as you want
set result Bitmap in ImageView and return result
Set your BitmapDisplayer into configuration.

Categories

Resources