Search code examples
androidarrayswallpaper

Set Wallpaper when images are from arraylist


How could i set wallpaper when the images are stored in arraylist i have tried but no hope!!

here is the code:

final ImageView image = new ImageView(mContext);
image.setImageResource(mImageIds[pos]);                 

try {
    wallpaperManager.setBitmap(image.getDrawingCache());
} catch (IOException e) {
    e.printStackTrace();
}

I tried using above code but it showing NullPointer Exception.


Solution

  • you could've showed a bit more code (how is your array, string name of picture or int id). anyway ill show you how i did similiar thing:

    Bitmap bitmap;
        final int[] imgs = new int[] { R.drawable.i1, R.drawable.i2, R.drawable.i3,
                R.drawable.i4, R.drawable.i5, R.drawable.i6, R.drawable.i7,
                R.drawable.i8, R.drawable.i9, R.drawable.i10, };
    
    bitmap = BitmapFactory.decodeResource(getResources(), 
                        imgs[pos]);         
                try {
                    getApplicationContext().setWallpaper(bitmap);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }