我在一个drawable文件夹中有一组图像。我有一个按钮将图像设置为设备屏幕上的壁纸。但是,当我将此图像设置为壁纸时,无论是缩放还是裁剪。我想图像应该适合屏幕尺寸。我在SO上看过很多链接,但没有链接对我有用。这是我到目前为止所尝试的代码。
代码 -
Bitmap bmap2 = BitmapFactory.decodeStream(getResources().openRawResource(mThumb[position]));
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels;
int width = metrics.widthPixels;
Bitmap bitmap = Bitmap.createScaledBitmap(bmap2, width, height, true);
WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
try {
wallpaperManager.setBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
我还在清单中添加了以下行 -
<uses-permission android:name="android.permission.SET_WALLPAPER_HINTS"/>
<uses-permission android:name="android.permission.SET_WALLPAPER" />
答案 0 :(得分:23)
您好这是在使用可绘制的图像我已经检查了它..
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels;
int width = metrics.widthPixels;
Bitmap tempbitMap = BitmapFactory.decodeResource(getResources(), R.drawable.img);
Bitmap bitmap = Bitmap.createScaledBitmap(tempbitMap,width,height, true);
WallpaperManager wallpaperManager = WallpaperManager.getInstance(MainActivity.this);
wallpaperManager.setWallpaperOffsetSteps(1, 1);
wallpaperManager.suggestDesiredDimensions(width, height);
try {
wallpaperManager.setBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
在Manifest.xml中也提到这些权限..
<uses-permission android:name="android.permission.SET_WALLPAPER_HINTS" />
<uses-permission android:name="android.permission.SET_WALLPAPER" />
这是截图..
重置适合屏幕的壁纸存储共享首选项中的图像路径并使用Boot Completed Receiver然后在屏幕上重置相同的壁纸....
广播接收器是..
import java.io.IOException;
import android.app.WallpaperManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.WindowManager;
public class BootReceiver extends BroadcastReceiver {
private static final String TAG="BootReceiver";
@Override public void onReceive(Context context,Intent intent){
try{
DisplayMetrics metrics = new DisplayMetrics();
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
windowManager.getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels;
int width = metrics.widthPixels;
Bitmap tempbitMap = BitmapFactory.decodeResource(context.getResources(), R.drawable.img);
Bitmap bitmap = Bitmap.createScaledBitmap(tempbitMap,width,height, true);
WallpaperManager wallpaperManager = WallpaperManager.getInstance(context);
wallpaperManager.setWallpaperOffsetSteps(1, 1);
wallpaperManager.suggestDesiredDimensions(width, height);
try {
wallpaperManager.setBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}catch(Exception e){
Log.e(TAG,e.toString());
}
}
}
在Manifest.xml中添加这些行之后
<receiver
android:name=".BootReceiver"
android:enabled="true"
android:exported="true"
android:label="BootReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" >
</action>
</intent-filter>
</receiver>
答案 1 :(得分:3)
试试这段代码
Bitmap bmap2 =BitmapFactory.decodeStream(getResources().openRawResource(mThumb[position]));
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels;
int width = metrics.widthPixels;
Bitmap bitmap = Bitmap.createScaledBitmap(bmap2, width, height, true);
WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
wallpaperManager.setWallpaperOffsetSteps(1, 1);
wallpaperManager.suggestDesiredDimensions(width, height);
try {
wallpaperManager.setBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
答案 2 :(得分:2)
如果您有图片网址,请使用:
WallpaperManager wpm = WallpaperManager.getInstance(context);
InputStream ins = new URL("absolute/path/of/image").openStream();
wpm.setStream(ins);
如果您有图片URI,请使用
WallpaperManager wpm = WallpaperManager.getInstance(context);
wpm.setResource(Uri.of.image);
在您的清单文件中:
<uses-permission android:name="android.permission.SET_WALLPAPER"></uses-permission>
并且不要忘记将此代码放在AsyncTask
。
答案 3 :(得分:0)
试试这段代码:
public void changeWallpaper(String path) {
FileInputStream is;
BufferedInputStream bis;
WallpaperManager wallpaperManager;
Drawable wallpaperDrawable;
File sdcard = Environment.getExternalStorageDirectory();
try {
is = new FileInputStream(new File(path));
bis = new BufferedInputStream(is);
Bitmap bitmap = BitmapFactory.decodeStream(bis);
Bitmap useThisBitmap = Bitmap.createBitmap(bitmap);
wallpaperManager = WallpaperManager.getInstance(getActivity());
wallpaperDrawable = wallpaperManager.getDrawable();
wallpaperManager.setBitmap(useThisBitmap);
} catch (Exception e) {
e.printStackTrace();
}
}
在此示例中,我使用了device SD
卡中的图片...
这对我来说非常有用..
答案 4 :(得分:0)
注意:也许不适合你。
你需要这个:
WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
try {
wallpaperManager.setBitmap(bitmap);
wallpaperManager.suggestDesiredDimensions(width, height);
Toast.makeText(this, "Wallpaper Set", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}