Wednesday 1 August 2012

Set wallpaper image from Gallery programmatically in Android

Follow the stapes and intregrate the pice of codes in your application :
1.  final int RESULT_LOAD_IMAGE=1;

2.call this intent to open gallery and select an Image:
Intent i = new Intent(Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(i, lodImage);

3. Now on Activity result you can get the selected image path and you can set the Wallpaper :

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
         
        /*
         * if request made to select a image from gallery
         */
        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
           String wallpaper= selectedWallpaperImage(getApplicationContext(),data);
           WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
           
           try {
wallpaperManager.setBitmap(BitmapFactory.decodeFile(wallpaper));
                         //this line of code will set the selected image as your wallpaper...........
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
        }
    }

4.You need this method to get the Gallery image path

public static String selectedWallpaperImage(Context context,Intent data){
Uri selectedImage = data.getData();
     String[] filePathColumn = { MediaStore.Images.Media.DATA };

     Cursor cursor = context.getContentResolver().query(selectedImage,
             filePathColumn, null, null, null);
     cursor.moveToFirst();

     int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
     String picturePath = cursor.getString(columnIndex);
     cursor.close();
return picturePath;
}

3 comments:

  1. Hii,Sourav,Can you send source code of set wallpaper image from gallery programmatically.
    My mail id swarup.gosavi@gmail.com

    ReplyDelete
  2. https://whatsappvideosstatuss.blogspot.in/2017/12/tamil-heroine-samntha.html

    ReplyDelete