这是我的壁纸设置代码,但我想将壁纸保存到SD卡。给我一个示例代码。我尝试下面的代码来保存壁纸,但它不起作用。您可以在给定代码中看到该代码。 “case R.id.save:”
ImageView display, p, c1,s8,s9,s10;
Button s;
int to;
Button set;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_kitkat);
to = R.drawable.n1;
display = (ImageView) findViewById(R.id.kitkatimageview);
c1 = (ImageView) findViewById(R.id.n1);
s8 = (ImageView) findViewById(R.id.s8);
s9 = (ImageView) findViewById(R.id.s9);
s10 = (ImageView) findViewById(R.id.s10);
p = (ImageView) findViewById(R.id.p);
s = (Button) findViewById(R.id.save);
set = (Button) findViewById(R.id.set);
c1.setOnClickListener(this);
p.setOnClickListener(this);
s8.setOnClickListener(this);
s9.setOnClickListener(this);
s10.setOnClickListener(this);
s.setOnClickListener(this);
set.setOnClickListener(this);
display.setImageResource(R.drawable.n1);
}
@SuppressWarnings("deprecation")
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.n1:
display.setImageResource(R.drawable.n1);
to = R.drawable.n1;
break;
case R.id.p:
display.setImageResource(R.drawable.p);
to = R.drawable.p;
break;
case R.id.s8:
display.setImageResource(R.drawable.s8);
to = R.drawable.s8;
break;
case R.id.s9:
display.setImageResource(R.drawable.s9);
to = R.drawable.s9;
break;
case R.id.s10:
display.setImageResource(R.drawable.s10);
to = R.drawable.s10;
break;
case R.id.save:
/*InputStream in = getResources().openRawResource(to);
Bitmap bmp = BitmapFactory.decodeStream(in);
MediaStore.Images.Media.insertImage(getContentResolver(), bmp,
"n1", "saved");
Toast.makeText(this, "Wallpaper saved!", Toast.LENGTH_LONG).show();
break;*/
case R.id.set:
InputStream y1 = getResources().openRawResource(to);
Bitmap b1 = BitmapFactory.decodeStream(y1);
WallpaperManager myWallpaperManager = WallpaperManager
.getInstance(getApplicationContext());
try {
myWallpaperManager.setResource(to);
Toast.makeText(this, "Wallpaper Set!", Toast.LENGTH_LONG)
.show();
} catch (IOException e) {
try {
getApplicationContext().setWallpaper(b1);
Toast.makeText(this, "Wallpaper Set!",Toast.LENGTH_SHORT)
.show(); } catch (IOException e) {
e.printStackTrace();
}
break;
}
}
}
答案 0 :(得分:0)
试试这个
Bitmap bm = BitmapFactory.decodeResource(getResources(), to);
File myDir = new File("/mnt/sdcard/");
String fname = "filename"+".png";
File file = new File (myDir,fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}