set image from server as wallpaper with full size - android

i'm using these codes to set my image from server as wallpaper :
WallpaperManager wpm = WallpaperManager.getInstance(MainActivity.this);
InputStream ins;
try {
String xlx = "http://myserver.com/myimage.jpg";
ins = new URL(xlx).openStream();
wpm.setStream(ins);
} catch (IOException e) {
e.printStackTrace();
}
it's working and my image will set as wallpaper,
but my problem is that it is not setting full image as wallpaper, so how should i do to set full image as wallpaper without cropping?

set imageview property in your layout xml file.
android:scaleType="fitXY"

Related

Android. Can't decode Bitmap from stream

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);

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 local photo as wallpaper on Android

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.

Fb profile pic not coming with a better quality

I have used the following code, able to get the image but when I am trying to put it on Layout background(Linear Layout), the image quality gets reduced and it shows a blurred image.
URL img_value = null;
try {
img_value = new URL("http://graph.facebook.com/"+userId+"/picture?type=large");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Bitmap mIcon1 = null;
try {
BitmapFactory.Options opt=new BitmapFactory.Options();
opt.inSampleSize=1;
// mIcon1 = BitmapFactory.decodeFile(pathoffile,opt);
mIcon1 = BitmapFactory.decodeStream(img_value.openConnection().getInputStream(),null,opt);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ll=(LinearLayout) findViewById(R.id.ll);
BitmapDrawable background = new BitmapDrawable(mIcon1);
ll.setBackgroundDrawable(background);
It looks like you're trying to set that image to the background of a LinearLayout. I am going to assume that this background is roughly the size of the phone's screen.
I just tried out that facebook URL and the size of the image I am getting back is 180x195.
Considering the smallest Android screen I am aware of is 240x320, that image will have to be blown up on every single Android phone. This will cause the image to look blurry, as the resolution of the image is much lower than that of Android screens.
I recommend you find higher resolution images for your backgrounds.

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