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(); }

下面是实现效果:

更换壁纸后:

具体代码为:

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; } }

更多相关文章

  1. android AsyncTask
  2. Android之手机壁纸的改变
  3. android图片缩放手势检测类--ScaleGestureDetector
  4. android图片压缩方法
  5. android ListView SimpleAdapter 带图片
  6. 【Android】Android内存溢出问题---用自行开辟的空间进行对内存
  7. android -上传文件到服务器
  8. Android之OnGestureListener实现图片的左右滑动
  9. android 动态壁纸

随机推荐

  1. android log输出控制
  2. Android之获取手机上的图片和视频缩略图t
  3. Android(安卓)shape 矩形边框
  4. Android横竖屏切换生命周期你真的了解么?
  5. 史上最强劲之android模拟器命令详解
  6. Android技术积累汇总(4月28日更新)
  7. Android(安卓)通知栏自定义样式
  8. rk3288 Android(安卓)5.1root方法
  9. Android中的文件存储
  10. android studio3.1.4更新3.2.1版本问题汇