0

这是我的壁纸设置代码,但我想将壁纸保存到 SD 卡。给我一个示例代码。我试过下面的代码来保存墙纸,但它不起作用。您可以在给定的代码中看到该代码。“案例 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;
        }
    }

}
4

1 回答 1

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();
      }
于 2013-10-06T07:28:14.767 回答