Monday, April 8, 2013

[android help] How can i set Wallpaper in android using coding?

How can i set Wallpaper in android using coding? - Stack Overflow



















I am developing an application that shows the different photos from server and user can set selected photos as wallpaper of its device i used given code to set wallpaper it working but image was not set properly it not fit to screen. I used this code.



String dirPath = getFilesDir().toString();

String folder = mPhotos.get(nextPosition - 1).getCategory();
String filePath = dirPath + "/PhotoViewer/" + folder + "/"
+ mPhotos.get(nextPosition - 1).getFileName();
File imageFile = new File(filePath);
Bitmap bitmap = BitmapFactory.decodeFile(imageFile
.getAbsolutePath());
WallpaperManager myWallpaperManager = WallpaperManager
.getInstance(getApplicationContext());
try {
myWallpaperManager.setBitmap(bitmap);
Toast.makeText(PhotoActivity.this, "Wallpaper set",
Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(PhotoActivity.this, "Error setting wallpaper",
Toast.LENGTH_SHORT).show();
}


Please help me out.


























You can try resizing your bitmap like this



DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels << 1; // best wallpaper width is twice screen width

// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);

// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, width, height);

// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
Bitmap decodedSampleBitmap = BitmapFactory.decodeFile(path, options);

WallpaperManager wm = WallpaperManager.getInstance(this);
try {
wm.setBitmap(decodedSampleBitmap);
} catch (IOException e) {
Log.e(TAG, "Cannot set image as wallpaper", e);
}






















To set wallpaper in android use below code: By using WallpaperManager Class



Button buttonSetWallpaper = (Button)findViewById(R.id.set);
ImageView imagePreview = (ImageView)findViewById(R.id.preview);
imagePreview.setImageResource(R.drawable.five);

buttonSetWallpaper.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
WallpaperManager myWallpaperManager
= WallpaperManager.getInstance(getApplicationContext());
try {
myWallpaperManager.setResource(R.drawable.five);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}});



Need to set permission in Manifest:





















default






.

stackoverflow.comm

1 comment:

  1. Hello,
    how to use this code for set wallpaper from my site/gallery web? and not from local dir?

    Thanks.

    ReplyDelete

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...