4

Below are my codes, I want to use wallpaper manager to set as wallpaper. I'm using Universal Image Loader, but i dont know how implement wallpaper manager. My setWall() is not working, kinda confusing.

    import android.graphics.Bitmap;
    import android.os.Bundle;
    import android.os.Parcelable;
    import android.support.v4.view.PagerAdapter;
    import android.support.v4.view.ViewPager;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.animation.Animation;
    import android.view.animation.AnimationUtils;
    import android.widget.ImageView;
    import android.widget.ProgressBar;
    import android.widget.Toast;

    import com.nostra13.universalimageloader.core.DisplayImageOptions;
    import com.nostra13.universalimageloader.core.assist.FailReason;
    import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;
    import com.nostra13.universalimageloader.core.assist.ImageScaleType;


    public class ImageActivity extends BaseActivity {

        private DisplayImageOptions imageoptions;

        private ViewPager imagepager;

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

            setContentView(R.layout.imagepager);


            Bundle bundle = getIntent().getExtras();
            String[] imageUrls = bundle.getStringArray(Extra.IMAGES);
            int pagerPosition = bundle.getInt(Extra.IMAGE_POSITION, 0);
            imageoptions = new DisplayImageOptions.Builder()
                .showImageForEmptyUri(R.drawable.noimage)
                .cacheOnDisc()
                .imageScaleType(ImageScaleType.EXACT)
                .build();

            imagepager= (ViewPager) findViewById(R.id.imagepager);
            imagepager.setAdapter(new ImagePagerAdapter(imageUrls));
            imagepager.setCurrentItem(pagerPosition);
        }


 public void setWall() {

    WallpaperManager myWallpaperManager
     = WallpaperManager.getInstance(getApplicationContext());
    try {
     myWallpaperManager.setResource(R.drawable.app_icon); //<--My app just set my app icon image as wallpaper, this is not I wanted. I wanted to set my selected image as wallpaper 
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }

}

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.setWall:
                setWall();
                return true;
            default:
                return false;
        }
    }

@Override
    protected void onStop() {
        imageLoader.stop();
        super.onStop();
    }

    private class ImagePagerAdapter extends PagerAdapter {

        private String[] images;
        private LayoutInflater inflater;

        ImagePagerAdapter(String[] images) {
            this.images = images;
            inflater = getLayoutInflater();
        }

        @Override
        public void destroyItem(View container, int position, Object object) {
            ((ViewPager) container).removeView((View) object);
        }

        @Override
        public void finishUpdate(View container) {
        }

        @Override
        public int getCount() {
            return images.length;
        }

        @Override
        public Object instantiateItem(View view, int position) {
            final View imageLayout = inflater.inflate(R.layout.item_pager_image, null);
            final ImageView imageView = (ImageView) imageLayout.findViewById(R.id.image);
            final ProgressBar spinner = (ProgressBar) imageLayout.findViewById(R.id.loading);

            imageLoader.displayImage(images[position], imageView, imageoptions, new ImageLoadingListener() {
                public void onLoadingStarted() {
                    spinner.setVisibility(View.VISIBLE);
                }

                public void onLoadingFailed(FailReason failReason) {
                    String message = null;
                    switch (failReason) {
                        case IO_ERROR:
                            message = "Input/Output error";
                            break;
                        case OUT_OF_MEMORY:
                            message = "Out Of Memory error";
                            break;
                        case UNKNOWN:
                            message = "Unknown error";
                            break;
                    }
                    Toast.makeText(ImagePagerActivity.this, message, Toast.LENGTH_SHORT).show();

                    spinner.setVisibility(View.GONE);
                    imageView.setImageResource(android.R.drawable.ic_delete);
                }

                public void onLoadingComplete(Bitmap loadedImage) {
                    spinner.setVisibility(View.GONE);
                    Animation anim = AnimationUtils.loadAnimation(ImagePagerActivity.this, R.anim.fade_in);
                    imageView.setAnimation(anim);
                    anim.start();
                }

                public void onLoadingCancelled() {
                    // Do nothing
                }
            });

            ((ViewPager) view).addView(imageLayout, 0);
            return imageLayout;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view.equals(object);
        }

        @Override
        public void restoreState(Parcelable state, ClassLoader loader) {
        }

        @Override
        public Parcelable saveState() {
            return null;
        }

        @Override
        public void startUpdate(View container) {
        }
    }





}
Android Novice
  • 536
  • 3
  • 14
  • 33
  • My question for you is, what are you *expecting* `setWall()` to do? You're giving it `0` as the resource value, not an actual resource ID. You also need to provide information on what you mean by "not working" -- is it crashing? Are you getting an unexpected result? If it's crashing, look at the LogCat and see the exception details. If you still don't understand, edit the log from LogCat into your question. – Kevin Coppock Aug 14 '12 at 13:35
  • I've edited my codes and here's my logcat. – Android Novice Aug 14 '12 at 13:54
  • Yes, What's that `R.id.imagepager` This is not a solution. Just do that with any `R.drawable.someimageresource` For this, Just type in your workspace like `R.drawable.` and after use `Ctrl + spacebar` it will display your images. Otherwise, try app launch icon with `R.drawable.ic_launcher` or `R.drawable.icon` It depends on you SDK. Or just try this `R.drawable.noimage` – Praveenkumar Aug 14 '12 at 14:00
  • SPK, My app just set my app icon image as wallpaper, this is not I wanted. I wanted to set my selected image as wallpaper. I'm using a viewpager for my app . Then uses option menu to set image as wallpaper – Android Novice Aug 14 '12 at 14:16

2 Answers2

8

Instead of myWallpaperManager.setResource(0); Why didn't you use the myWallpapaerManager.setResource(R.drawable.yourimage)

Have a look at Wallpapaer Manager example. Hope this helps you lot.

Praveenkumar
  • 24,084
  • 23
  • 95
  • 173
  • Thanks, but I'm already using that example now. But I have no idea how to implement it. What should i put into (R.drawable.yourimage)? Can you show an example according to my codes? – Android Novice Aug 14 '12 at 13:37
  • @user1598107 You don't have any images under the folder `res/drawable-ldpi` (or) `res/drawable-mdpi` (or) `res/drawable/hdpi` Use anyone of that from there. – Praveenkumar Aug 14 '12 at 13:39
0

You should convert your Drawable to a Bitmap and then use the setBitmap() function of wallpaper manager to set your desired wallpaper

Convert Drawable to Bitmap

private Bitmap getBitmapFromDrawable(Drawable d){
         Bitmap bitmap = 
                 ((BitmapDrawable) d).getBitmap();
         return bitmap;
}

Set as wallpaper

public void setWall() { 
    WallpaperManager myWallpaperManager = 
         WallpaperManager.getInstance(getApplicationContext()); 
 try { 
    myWallpaperManager.setBitmap(getBitmapFromDrawable(R.drawable.app_icon); 
 } catch (IOException e) { 
    // TODO Auto-generated catch block e.printStackTrace(); 
 } 
} 

Hope this helps

Yugansh Tyagi
  • 646
  • 10
  • 24