I cannot change Android wallpaper using WallpaperManager - android

I am trying to change Android wallpaper using code. I am using the WallpaperManager class, but with no prevail. I used a .png image in the /drawable directory. I am getting an error that says, "Expected resource of type raw". When I run the application(when that method runs), it crashes. I must be victim of a really stupid mistake. The method changeWallpaper() is run after the user taps a button. Here is my code:
public void changeWallpaper(View view) {
try{
WallpaperManager wallpaperManager = WallpaperManager.getInstance(getApplicationContext());
wallpaperManager.setResource(R.drawable.material_wallpaper);
String successMessage = "Wallpaper Changes";
Toast.makeText(this, successMessage, Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
String failedMessage = "Operation failed";
Toast.makeText(this, failedMessage, Toast.LENGTH_SHORT).show();
}
}
EDIT: There is no "raw" folder in my /res/ directory.

If you want to stay with Drawable, you can convert the resource into a Bitmap and then set it as wallpaper by using setBitmap(Bitmap _bitmap) (see WallpaperManager).
WallpaperManager wallpaperManager = WallpaperManager.getInstance(getActivity().getApplicationContext());
Bitmap bitmap = BitmapFactory.decodeResource(getActivity().getResources(), R.drawable.material_wallpaper);
wallpaperManager.setBitmap(bitmap);

Related

How to crop an image getting from android resource folder(#mipmap)?

I have an image view pager which will select an image.But i want to crop this image with zoomIn and ZoomOut function in image.How I can do this?
mImageCaptureUri=Uri.parse("android.resource://app.islamic.unikacomp.myapplication/" +sliderImagesId[num]);
// Retrieve a WallpaperManager
WallpaperManager myWallpaperManager = WallpaperManager.getInstance(Animalwallpaper.this);
try {
// Change the current system wallpaper
myWallpaperManager.setResource(sliderImagesId[num]);
// Show a toast message on successful change
Toast.makeText(Animalwallpaper.this,"Wallpaper successfully changed", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
// TODO Auto-generated catch block
}
You can find multiple library on Github
Here are the few-
Android Image Cropper
Croper

Android - How to get current wallpaper name

I'm developing an application that sets wallpapers from com.android.launcher3 package drawable resources. At some point I need to check if the wallpaper is set correctly so I can move on to other step.
After some research in SO and googling, I wasn't able to find any information about getting current wallpaper name.
Here is how I set the drawable which I have no problem:
try {
WallpaperManager wallpaper_manager = WallpaperManager.getInstance(m_context);
Resources res = m_context.getPackageManager().getResourcesForApplication("com.android.launcher3");
int drawable_id = res.getIdentifier(wallpaper_name, "drawable", "com.android.launcher3");
Drawable drawable = res.getDrawable(drawable_id, null);
if(drawable != null) {
wallpaper_manager.setBitmap(((BitmapDrawable)drawable).getBitmap());
}
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
I can get the current wallpaper as drawable as well:
WallpaperManager wallpaper_manager = WallpaperManager.getInstance(m_context);
Drawable drawable = wallpaper_manager.getDrawable();
but I haven't managed to get current wallpaper name.
I need help.
Thanks in advance.
I would try this two options:
1. Using wallpaperManager
You shouold be able to get the information using this:
wallpaperManager.getWallpaperInfo();
This will return a WallpaperInfo object which contains all the data about the wallpaper.
More information
https://developer.android.com/reference/android/app/WallpaperManager.html
2. Getting the drawable file
You can also try to get the URI of the drawable like this:
String imageUri = "drawable://" + R.drawable.image;
And get the file name from there.
Hope it helps you.

Set wallpaper using bitmap in android

When setting the wallpaper I'm using an int and then I'm converting it into a bitmap with decodestream but the wallpaper is still not set.
Here is my code
InputStream y = getResources().openRawResource(friendship);
Bitmap b = BitmapFactory.decodeStream(y);
try {
getApplicationContext().setWallpaper(b);
Toast.makeText(this, "Wallpaper Set!", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
Is the permission SET_WALLPAPER set in the manifest file? Android docs link
Further more, the method that you are using is deprecated. You should be using WallpaperManager apis

creating a drawable from sd card to set as a background in android

I am trying to use an image from the sd card and set it as the background for a relativelayout. I have tried other solutions that i have found here and elsewhere but they havent seemed to work for me. here is my code. I have commented out other ways that i have tried and didnt work. the only thing that worked for me was using setBackgroudnResource and using a resource from the app, but this was just to test to make sure mRoot was set up correctly. when I have tried all the other ways, it just doesn't set anything. Anyone know what I am doing wrong, or if there is a better way to do this?
//one way i tired...
//String extDir = Environment.getExternalStorageDirectory().toString();
//Drawable d = Drawable.createFromPath(extDir + "/pic.png");
//mRoot.setBackgroundDrawable(d);
//another way tried..
//Drawable d = Drawable.createFromPath("/sdcard/pic.png");
//mRoot.setBackgroundDrawable(d);
//last way i tried...
mRoot.setBackgroundDrawable(Drawable.createFromPath(new File(Environment.getExternalStorageDirectory(), "pic.png").getAbsolutePath()));
//worked, only to verify mRoot was setup correctly and it could be changed
//mRoot.setBackgroundResource(R.drawable.bkg);
You do not load a drawable from SD card but a bitmap. Here is a method to load it with the reduced sampling (quality) so the program will not complain if the image is too large. Then I guess you need to process this bitmap i.e. crop it and resize for the background.
// Read bitmap from Uri
public Bitmap readBitmap(Uri selectedImage) {
Bitmap bm = null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2; //reduce quality
AssetFileDescriptor fileDescriptor =null;
try {
fileDescriptor = this.getContentResolver().openAssetFileDescriptor(selectedImage,"r");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
finally{
try {
bm = BitmapFactory.decodeFileDescriptor(fileDescriptor.getFileDescriptor(), null, options);
fileDescriptor.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return bm;
}
The Uri here can be supplied from a gallery picker activity.
The image then can be saved into application resources and loaded into an imageView
private void saveBackground(Bitmap Background) {
String strBackgroundFilename = "background_custom.jpg";
try {
Background.compress(CompressFormat.JPEG, 80, openFileOutput(strBackgroundFilename, MODE_PRIVATE));
} catch (Exception e) {
Log.e(DEBUG_TAG, "Background compression and save failed.", e);
}
Uri imageUriToSaveCameraImageTo = Uri.fromFile(new File(BackgroundSettings.this.getFilesDir(), strBackgroundFilename));
// Load this image
Bitmap bitmapImage = BitmapFactory.decodeFile(imageUriToSaveCameraImageTo.getPath());
Drawable bgrImage = new BitmapDrawable(bitmapImage);
//show it in a view
ImageView backgroundView = (ImageView) findViewById(R.id.BackgroundImageView);
backgroundView.setImageURI(null);
backgroundView.setImageDrawable(bgrImage);
}
File file = new File( url.getAbsolutePath(), imageUrl);
if (file.exists()) {
mDrawable = Drawable.createFromPath(file.getAbsolutePath());
}
I suggest checking that the drawable is being loaded correctly. Some things to try:
Try using a different image on the sd card
Put pic.png in R.drawable and make sure mRoot.setBackgroundResource() does what you expect
After loading the drawable, check d.getBounds() to make sure it is what you expect

Android - how to set the wallpaper image

Is it possible to set the android wallpaper image programatically? I'd like to create a service that downloads an image from the web and updates the home screen wallpaper periodically.
If you have image URL then use
WallpaperManager wpm = WallpaperManager.getInstance(context);
InputStream ins = new URL("absolute/path/of/image").openStream();
wpm.setStream(ins);
If you have image URI then use
WallpaperManager wpm = WallpaperManager.getInstance(context);
wpm.setResource(Uri.of.image);
In your manifest file:
<uses-permission android:name="android.permission.SET_WALLPAPER"></uses-permission>
From this page on the developer site:
public void setStream (InputStream data)
Change the current system wallpaper to a specific byte stream. The give InputStream is copied into persistent storage and will now be used as the wallpaper. Currently it must be either a JPEG or PNG image.
If you have bitmap of image than you will add this function to set as wallpaper:
public void SetBackground(int Url) {
try {
File file = new File("/sdcard/sampleimage");
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), Url);
bitmap.compress(CompressFormat.JPEG, 80, new FileOutputStream(file));
Context context = this.getBaseContext();
context.setWallpaper(bitmap);
Toast.makeText(getApplicationContext(), "Wallpaper has been set", Toast.LENGTH_SHORT).show();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
you should add permission for this
<uses-permission android:name="android.permission.SET_WALLPAPER"></uses-permission>
hope it will work
OK Here's how to do it before api 2.0:
You need to call getApplicationContext.setWallpaper() and pass it the bitmap.
This method is now deprecated. See ChrisF's answer for details on the new method.

Categories

Resources