Android Set as Wallpaper

Put your problem here if it does not fit any of the other categories.

Android Set as Wallpaper

Postby kcchai » Tue Aug 14, 2012 8:24 am

Please show me an example code on how to set image as wallpaper using Android WallpaperManager. I have shortened and edited my question. Hopefully you guys could understand my question. I will show some attempts I have made. Please show me how is it done. Thank you :mrgreen:

ImagePagerActivity.class
Code: Select all
public class ImagePagerActivity extends BaseActivity {

    private ViewPager pager;

    private DisplayImageOptions options;


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.ac_image_pager);
        Bundle bundle = getIntent().getExtras();
        String[] imageUrls = bundle.getStringArray(Extra.IMAGES);
        final int pagerPosition = bundle.getInt(Extra.IMAGE_POSITION, 0);

        options = new DisplayImageOptions.Builder()
            .showImageForEmptyUri(R.drawable.image_for_empty_url)
            .cacheOnDisc()
            .imageScaleType(ImageScaleType.EXACT)
            .build();

        pager = (ViewPager) findViewById(R.id.pager);
        pager.setAdapter(new ImagePagerAdapter(imageUrls));
        pager.setCurrentItem(pagerPosition);
    }
}
@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.set_wallpaper:
                setWallpaper();
                return true;
            default:
                return false;
        }
    }


1st Attempt (My pagerPosition is giving error "pagerPosition cannot be resolved to a variable")

Code: Select all
public void setWallpaper(){
             Bitmap bitmap = BitmapFactory.decodeResource(getResources(), pagerPosition);
    try {
         ImagePagerActivity.this.setWallpaper(bitmap);
            } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }
        Log.d("Gallery Example", "Image setted.");
            }


2nd Attempt (My pagerPosition is giving error "pagerPosition cannot be resolved to a variable")

Code: Select all
public void setWallpaper() {

        try {
            File file = new File("/sdcard/sampleimage");
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), pagerPosition);
            bitmap.compress(CompressFormat.JPEG, 80, new FileOutputStream(file));
            Context context = this.getBaseContext();
            context.setWallpaper(bitmap);           
            Toast.makeText(getApplicationContext(), "Wallpaper has been set", Toast.LENGTH_SHORT).show();           
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }         
    }

@Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
                case R.id.set_wallpaper:
                    setWallpaper();
                    return true;
                default:
                    return false;
            }
        }
kcchai
Once Poster
Once Poster
 
Posts: 1
Joined: Tue Aug 14, 2012 8:19 am

Top

Re: Android Set as Wallpaper

Postby Aseriesofsmallprojects » Fri Aug 17, 2012 1:03 am

The "pagerPosition" variable is not in scope in the setWallpaper method. You aren't passing it in, and it only exists in the onCreate method. Rethink how that data should be getting to where you need it.

I use WallpaperManager as such (with supporting code to create the bitmap, and exception handling):
Code: Select all
final WallpaperManager wallpaperManager = WallpaperManager.getInstance(context);
wallpaperManager.setBitmap(resized);
www.ProjectJourneyman.com - Android development and app marketing
twitter.com/PrjJourneyman Twitter feed
Android Developer Tutorial A growing tutorial for Android Devs
The Android Income Series Books on income topics for Android developers
Aseriesofsmallprojects
Developer
Developer
 
Posts: 39
Joined: Fri Oct 08, 2010 4:29 pm
Location: USA

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 14 guests