Android学习笔记(四) android 更换桌面壁纸-----WallpaperManager

Android 中壁纸管理主要由 WallpaperManager 这个类负责

 

 

Provides access to the system wallpaper. With WallpaperManager, you can get the current wallpaper, get the desired dimensions for the wallpaper, set the wallpaper, and more. Get an instance of WallpaperManager with getInstance().

 

 

他的初始化方法为

wallpaperManager = WallpaperManager.getInstance(this); 

 

获取当前壁纸的方法为:

Drawable drawable = wallpaperManager.getDrawable();

 

设置一张图片作为壁纸的方法为

 

 

BitmapDrawable source = new BitmapDrawable(path); try { wallpaperManager.setBitmap(source.getBitmap()); iv.setImageDrawable(source); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } 

 

下面是实现效果:

 

Android学习笔记(四) android 更换桌面壁纸-----WallpaperManager_第1张图片Android学习笔记(四) android 更换桌面壁纸-----WallpaperManager_第2张图片

 

更换壁纸后:

Android学习笔记(四) android 更换桌面壁纸-----WallpaperManager_第3张图片

 

 

具体代码为:

 

package young.MyWallPaperManager; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import javax.security.auth.PrivateCredentialPermission; import android.app.Activity; import android.app.WallpaperManager; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.view.ContextMenu; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ContextMenu.ContextMenuInfo; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.TextView; public class MyWallPaperManager extends Activity { private ImageView iv ; private Button changeImageButton; private TextView currentTextView; private ArrayList<File> images ; private WallpaperManager wallpaperManager; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //获得桌面壁纸 wallpaperManager = WallpaperManager.getInstance(this); Drawable drawable = wallpaperManager.getDrawable(); iv = (ImageView)findViewById(R.id.imview); currentTextView = (TextView)findViewById(R.id.currentwall); changeImageButton =(Button)findViewById(R.id.ChangeWallpaperButton); iv.setImageDrawable(drawable); //registerForContextMenu(changeImageButton); changeImageButton.setOnCreateContextMenuListener(this); //取得所有图片 images = new ArrayList<File>(); getAllFiles(new File("/sdcard")); } @Override public void onCreateContextMenu(ContextMenu menu,View view, ContextMenuInfo menuInfo){ menu.setHeaderTitle("点击设置为桌面壁纸"); for(int i = 0 ; i <images.size() ; i++){ menu.add(0, i, i, images.get(i).getName()); } } @Override public boolean onContextItemSelected(MenuItem item){ String name = (String) item.getTitle(); //根据名字取得图片 String path = getElementByName(name).getPath(); // BitmapDrawable source = new BitmapDrawable(path); try { //设置为壁纸 wallpaperManager.setBitmap(source.getBitmap()); //显示 iv.setImageDrawable(source); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return true; } //取得所有png 和 jpg jpeg private void getAllFiles(File root){ File files[] = root.listFiles(); if(files != null) for(File f:files){ if(f.isDirectory()){ getAllFiles(f); } else{ //检查文件类型 String fpath = f.getPath().substring(f.getPath().lastIndexOf(".")+1,f.getPath().length()); //如果是图片 if(fpath.equals("png")||fpath.equals("jpg")){ images.add(f); } } } } private File getElementByName(String Name){ for(int i = 0 ; i <images.size() ; i++){ if(images.get(i).getName().equals(Name)) return images.get(i); } return null; } }  

你可能感兴趣的:(android,String,File,null,Path,button)