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
Related
I have URL to PNG image file.
I want to get this image file and set it as a source for an ImageView.
My code:
URL iconURL = null;
try {
iconURL = new URL("https://maps.gstatic.com/mapfiles/place_api/icons/worship_general-71.png");
} catch (MalformedURLException e) {
e.printStackTrace();
}
Bitmap icon = null;
try {
icon = BitmapFactory.decodeStream(iconURL.openConnection().getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
myImageView.setImageBitmap(icon);
If I place a breakpoint into the line with decodeStream() the app stops and I see the following:
If I run the app in regular mode, it just stops!
Can someone explain what is wrong here and how to do it correctly?
Thanks.
If you just want to show the image on your ImageView then you can use Picasso Library.
Put below dependency in build.gradle file
'com.squareup.picasso:picasso:2.71828'
And use below java code for loading your image from URL to your ImageView
Picasso.get()
.load(url)
.into(imageView);
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);
I use below code to set photo as wallpaper:
try {
File f = new File(PhotoPath);
InputStream in = new FileInputStream(f);
this.setWallpaper(in);
} catch (IOException e) {
e.printStackTrace();
}
For some photo, it can set as wallpaper success.
But for some photo, the wallpaper will be set to device default wallpaper or all black.
Is there any limitation of the photo which be set to wallpaper?
Or this code how to modify to fix issue?
Try this method from this link : developer.
public void setStream (InputStream data);
Currently image must be either a JPEG or PNG. This method supports from API Level 5.
I am wondering how to get the bitmap from specific location as follows:
try {
Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL("C:/java/AVMOrderServer/files/main_ads/fff.jpg").getContent());
iv.setImageBitmap(bitmap);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Any advice would be useful (I found the code sample on stackoverflow which is an answer voted as true)
Thank you all.
Your Android device does not have a C drive, that's generally a DOS/Windows concept. Android is based on Linux. If you want to view a bitmap on your computer from your Android device you'll have to copy it over first, whether by including it in your APK or by copying to SD card.
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.