2013-10-31 89 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

傳遞一個整數請求代碼.... –