0

this is my wallpaper set code but I want to save wallpaper to sd card. Give me a sample code. i tried below code to save wallpaper but it does not work. You can see that code in given code. "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;
        }
    }

}
9477
  • 3
  • 6

1 Answers1

0

try like this

    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();
      }
Boopathi
  • 226
  • 1
  • 4
  • 18
  • Superb!! It works! But the only problem is that photos are saved in camera pictures,all picture, camera media all.. – 9477 Oct 07 '13 at 07:29
  • you can give the desired path inside the file path **File myDir = new File("/mnt/sdcard/");** – Boopathi Oct 07 '13 at 10:36
  • @user2789816 You could better use **Environment.getExternalStorage()** to get the exact sdcard path/location. Because in some mobiles the mounting location might be changed. – Ajeesh Oct 07 '13 at 10:40
  • It gives me a problem. If i use this code than photo is not saving in gallery. If saving than only 1 photo can be saved and when i open it photo gets changed. :( – 9477 Oct 08 '13 at 05:55
  • @user2789816 you should note the line **if (file.exists ()) file.delete ();** it will remove if the same file name exits – Boopathi Oct 08 '13 at 12:25