2010-02-05 92 views
7

可能重複:
Android - how to set the wallpaper imageAndroid - 如何設置壁紙圖像?

我想要做的是,使用圖像URI(不裁剪)

我在一個小白設置壁紙在Android和開發一般的開發。 互聯網已經失敗了我......提供代碼來設置壁紙。

的開發資源網站說

public void setStream (InputStream data)

,但我不明白,一些示例代碼,將極大地幫助我。

+0

相關:[Android的 - 如何設置壁紙圖像](http://stackoverflow.com/questions/1964193/android-how-to-set-the-wallpaper-image) – McDowell 2011-05-18 10:36:41

回答

3

如果您有圖像URL,則可以使用流(抽象)打開它表示的資源: new URL("your.image.url.com").openStream()。此方法調用將返回類型爲InputStream的對象,您可以將其作爲參數傳遞給setStream()方法。

如果您不想直接指定流,可以打開遠程流,創建一個位圖,然後使用WallpaperManager實例或執行context.setWallpaper(bitmap)(這已棄用)將您的位圖設置爲壁紙。

僅供參考請看this帖子。

+0

這是我目前的代碼.. InputStream is = getContentResolver()。openInputStream(imageUri); bgImage = BitmapFactory.decodeStream(is); 上下文context = this.getBaseContext(); context.setWallpaper(bgImage);在bgImage(行2和4)和getBaseContext()(第3行)' 錯誤 也最新一個URI和URL之間的差異? 我得到的簡短答案是「一個URL是一個URI,但是,一個URI不是一個URL」 – 2010-02-05 15:51:21

+0

好吧我修復了第2和第4行的錯誤,我沒有定義位圖bgImage。但仍然在getBaseContext上的錯誤() – 2010-02-05 17:01:42

+0

你知道你可以傳遞一個活動作爲上下文的實例嗎?沒有必要做一個this.getBaseContext()你可以傳遞當前活動或上下文對象的實例,如果你有一個[「this」將是一個有效的上下文對象] – Samuh 2010-02-08 05:42:46

11

嗨如果您有圖片路徑,您可以使用此代碼。

is = new FileInputStream(new File(imagePath)); 
bis = new BufferedInputStream(is); 
Bitmap bitmap = BitmapFactory.decodeStream(bis); 
Bitmap useThisBitmap = Bitmap.createScaledBitmap(
    bitmap, parent.getWidth(), parent.getHeight(), true); 
bitmap.recycle(); 
if(imagePath!=null){ 
    System.out.println("Hi I am try to open Bit map"); 
    wallpaperManager = WallpaperManager.getInstance(this); 
    wallpaperDrawable = wallpaperManager.getDrawable(); 
    wallpaperManager.setBitmap(useThisBitmap); 

,如果你有像然後使用URI這個

wallpaperManager = WallpaperManager.getInstance(this); 
wallpaperDrawable = wallpaperManager.getDrawable(); 
mImageView.setImageURI(imagepath); 

讓我知道如果有任何問題。

+0

這條線的用法是什麼? wallpaperDrawable = wallpaperManager.getDrawable(); – 2014-02-25 16:11:04