android 获得墙纸
package com.sn.android.image;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.app.WallpaperManager;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class ImageDemoActivity extends Activity {
private Button myBtn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myBtn = (Button) findViewById(R.id.btn_001);
myBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
WallpaperManager wallpaperManager = WallpaperManager
.getInstance(ImageDemoActivity.this);
Drawable d = wallpaperManager.getDrawable();
if (d != null) {
if (saveWP("shi", d)) {
Toast.makeText(ImageDemoActivity.this, "Save ok",
Toast.LENGTH_SHORT);
}
}
}
});
}
public boolean saveWP(String filename, Drawable drawble) {
try {
saveMyBitmap(filename, drawableToBitmap(drawble));
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
public static Bitmap drawableToBitmap(Drawable drawable) {
Bitmap bitmap = Bitmap.createBitmap(
drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(),
drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
canvas.setBitmap(bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight());
drawable.draw(canvas);
return bitmap;
}
public static void saveMyBitmap(String bitName, Bitmap mBitmap) {
File f = new File("/sdcard/" + bitName + ".png");
try {
f.createNewFile();
} catch (IOException e) {
Log.e("vsc-sxz", "在保存图片时出错:" + e.toString());
}
FileOutputStream fOut = null;
try {
fOut = new FileOutputStream(f);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
try {
fOut.flush();
} catch (IOException e) {
e.printStackTrace();
}
try {
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}