2013-10-31 51 views
0

如何设置图像壁纸全图像背景屏幕。我的代码Android-设置壁纸我的完整图像

Bitmap bmap = BitmapFactory.decodeResource(getResources(), 
        R.drawable.splash); 

      DisplayMetrics outMetrics = new DisplayMetrics(); 
      getWindowManager().getDefaultDisplay().getMetrics(outMetrics); 
      int w = outMetrics.widthPixels; 
      int h = outMetrics.heightPixels; 
      Bitmap wallpaper=Bitmap.createScaledBitmap(bmap, w, h, true); 

          WallpaperManager m = WallpaperManager.getInstance(getApplicationContext()); 
      try { 
       m.setBitmap(wallpaper); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

image

image

我需要像全套墙纸

+0

是从我的回答任何帮助。 –

回答

1
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(MainActivity.this); 
         wallpaperDrawable = wallpaperManager.getDrawable(); 
         wallpaperManager.setBitmap(useThisBitmap); 
       } catch (Exception e) { 
         e.printStackTrace(); 
       } 
    } 

上面的代码工作对我来说 而且不要忘了补充

<uses-permission android:name="android.permission.SET_WALLPAPER" /> 

在AndroidManifest.xml文件

您可以将图形内容转换成位图也

BitmapFactory.decodeResource(context.getResources(), R.drawable.icon_name); 
+0

如何为我的资源图片路径? – bmurat

+0

我编辑了我的答案并将新代码放入其中。请检查 –

+0

不要工作不要更改 – bmurat

0

您可以开始活动结果发布作物意图和结果进行检索,然后用壁纸管理器类。像这样:

Uri imgUri=Uri.parse("android.resource://your.package.name/"+R.drawable.image); 
Intent intent = new Intent("com.android.camera.action.CROP"); 
intent.setDataAndType(imgUri, "image/*"); 
intent.putExtra("crop", "true"); 
intent.putExtra("aspectX", 1); 
intent.putExtra("aspectY", 1); 
intent.putExtra("outputX", 80); 
intent.putExtra("outputY", 80); 
intent.putExtra("return-data", true); 
startActivityForResult(intent, REQUEST_CODE_CROP_PHOTO); 

,并使用壁纸经理在onResult功能

+0

REQUEST_CODE_CROP_PHOTO无法解析为变量 – bmurat

+0

传递一个整数请求代码.... –