Example usage for android.app WallpaperManager setBitmap

List of usage examples for android.app WallpaperManager setBitmap

Introduction

In this page you can find the example usage for android.app WallpaperManager setBitmap.

Prototype

@RequiresPermission(android.Manifest.permission.SET_WALLPAPER)
public void setBitmap(Bitmap bitmap) throws IOException 

Source Link

Document

Change the current system wallpaper to a bitmap.

Usage

From source file:com.dunrite.xpaper.utility.Utils.java

/**
 * Applies wallpaper to device's home screen
 *
 * @param c application's conext/*ww  w. j  a  v a  2s . c om*/
 * @param d drawable to apply to background
 */
public static void applyWallpaper(Context c, Drawable d) {
    WallpaperManager wm = WallpaperManager.getInstance(c);
    try {
        Bitmap bitmap = ((BitmapDrawable) d).getBitmap();
        //Testing setStream
        //ByteArrayOutputStream out = new ByteArrayOutputStream();
        //bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
        //InputStream inputStream = new ByteArrayInputStream(out.toByteArray());
        //Testing setStream
        if (bitmap != null) {
            wm.setBitmap(bitmap);
            //Testing setStream
            //wm.setStream(inputStream);
            bitmap.recycle();
            //Testing setStream
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:jahirfiquitiva.iconshowcase.tasks.ApplyWallpaper.java

@Override
protected Boolean doInBackground(Void... params) {
    Boolean worked = false;//from  w w  w .  j a  v a2 s .  c om
    if ((!wasCancelled) && (activity != null)) {
        WallpaperManager wm = WallpaperManager.getInstance(activity);
        try {
            try {
                wm.setBitmap(scaleToActualAspectRatio(resource));
            } catch (OutOfMemoryError ex) {
                if (ShowcaseActivity.DEBUGGING)
                    Utils.showLog(activity, "OutOfMemoryError: " + ex.getLocalizedMessage());
                showRetrySnackbar();
            }
            worked = true;
        } catch (IOException e2) {
            worked = false;
        }
    } else {
        worked = false;
    }
    return worked;
}

From source file:com.intellisol.plugin.Wallpaper.java

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    try {//from w  w w  .j av  a  2s.c  om
        // get path from argument
        String path = args.getString(0);

        // get context (Android)
        Context ctxt = cordova.getActivity().getBaseContext();
        WallpaperManager wallpaperManager = WallpaperManager.getInstance(ctxt);

        // get image file
        InputStream bitmapIn = cordova.getActivity().getAssets().open(path);
        Bitmap bitmap = BitmapFactory.decodeStream(bitmapIn);

        // set wallpaper
        wallpaperManager.setBitmap(bitmap);

    } catch (JSONException e) {
        // log error
        Log.d("Wallpaper", e.toString());
        return false;
    } catch (Exception e) {
        // log error
        Log.d("Wallpaper", e.toString());
        return false;
    }
    return true;
}

From source file:com.example.wojtekswiderski.woahpaper.GcmIntentService.java

public boolean setWallPaper(int start) {
    DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
    int height = displayMetrics.heightPixels;
    int width = displayMetrics.widthPixels;

    String url = "https://ajax.googleapis.com/ajax/services/search/images?v=1.0&rsz=1&q=" + word + "&start="
            + start;//from ww w  .  ja v  a  2  s.  co m
    String imageUrl;
    try {
        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();

        con.setRequestMethod("GET");

        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        JSONObject deliverable;

        try {
            deliverable = new JSONObject(response.toString());
            imageUrl = deliverable.getJSONObject("responseData").getJSONArray("results").getJSONObject(0)
                    .getString("url");
            Log.i(TAG, imageUrl);
            URL imageObj = new URL(imageUrl);

            Bitmap bmp = BitmapFactory.decodeStream(imageObj.openConnection().getInputStream());

            int x = bmp.getWidth();
            int y = bmp.getHeight();
            double ratioX = ((double) x) / ((double) width);
            double ratioY = ((double) y) / ((double) height);

            Log.i(TAG, "Width: " + width + " Height: " + height);
            Log.i(TAG, "X: " + x + " Y: " + y);
            Log.i(TAG, "RatioX: " + ratioX + " RatioY: " + ratioY);

            if (ratioX > ratioY) {
                bmp = Bitmap.createScaledBitmap(bmp, (int) (((double) x) / ratioY), height, false);
            } else {
                bmp = Bitmap.createScaledBitmap(bmp, width, (int) (((double) y) / ratioX), false);
            }

            Log.i(TAG, "newX: " + bmp.getWidth() + " newY: " + bmp.getHeight());

            Bitmap bmpBack = Bitmap.createBitmap(getWallpaperDesiredMinimumWidth(),
                    getWallpaperDesiredMinimumHeight(), Bitmap.Config.ARGB_8888);
            Bitmap bmOverlay = Bitmap.createBitmap(bmpBack.getWidth(), bmpBack.getHeight(),
                    bmpBack.getConfig());

            Canvas canvas = new Canvas(bmOverlay);
            canvas.drawBitmap(bmpBack, new Matrix(), null);
            canvas.drawBitmap(bmp, getWallpaperDesiredMinimumWidth() / 2 - bmp.getWidth() / 2, 0, null);

            WallpaperManager wpm = WallpaperManager.getInstance(this);
            wpm.setBitmap(bmOverlay);
        } catch (JSONException ex) {
            Log.e(TAG, "Could not convert to object");
            ex.printStackTrace();
            return true;
        }
    } catch (MalformedURLException ex) {
        ex.printStackTrace();
        Log.e(TAG, "Wrong url");
        return true;
    } catch (IOException ey) {
        ey.printStackTrace();
        Log.e(TAG, "Server down");
        return true;
    }
    return false;
}

From source file:com.leo.runningman.ui.ImageDetailActivity.java

private void setWapaper(Bitmap bitmap) {
    try {//from   w  w w  .ja va 2  s  . com
        WallpaperManager wpm = WallpaperManager.getInstance(this);
        float minH = wpm.getDesiredMinimumHeight();
        float minW = wpm.getDesiredMinimumWidth();
        Bitmap targetBitmap = getResizedBitmap(bitmap, (int) minH, (int) minW);
        wpm.setBitmap(targetBitmap);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:org.meepo.sexygirl.ImagePagerActivity.java

private void setWallpaper(final Image image) {
    new AsyncTask<String, Void, Void>() {
        @Override//from   w  ww.  ja  v a  2s .c o  m
        protected Void doInBackground(String... strings) {
            WallpaperManager wallpaperManager = WallpaperManager.getInstance(ImagePagerActivity.this);
            try {
                URL url = new URL(image.getUrl());
                Bitmap bitmap = BitmapFactory.decodeStream(url.openStream());
                wallpaperManager.setBitmap(bitmap);
                ImagePagerActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(ImagePagerActivity.this, "?", Toast.LENGTH_SHORT).show();
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
                ImagePagerActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(ImagePagerActivity.this, "", Toast.LENGTH_SHORT).show();
                    }
                });
            }
            return null;
        }
    }.execute();
}

From source file:de.baumann.thema.FragmentWallpaper.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.wallpaper, container, false);

    setHasOptionsMenu(true);//w  w  w. ja v  a  2  s. com

    final String[] itemTITLE = { getString(R.string.amber) + " - Lollipop",
            getString(R.string.blue) + " - Lollipop", getString(R.string.green) + " - Lollipop",
            getString(R.string.grey) + " - Lollipop", getString(R.string.red) + " - Lollipop",
            getString(R.string.teal) + " - Lollipop",

            getString(R.string.amber) + " - Marshmallow", getString(R.string.blue) + " - Marshmallow",
            getString(R.string.green) + " - Marshmallow", getString(R.string.grey) + " - Marshmallow",
            getString(R.string.red) + " - Marshmallow", getString(R.string.teal) + " - Marshmallow",

            getString(R.string.amber) + " - Nougat", getString(R.string.blue) + " - Nougat",
            getString(R.string.green) + " - Nougat", getString(R.string.grey) + " - Nougat",
            getString(R.string.red) + " - Nougat", getString(R.string.teal) + " - Nougat", };

    final String[] itemDES = {
            "CC license: https://github.com/scoute-dich/Blue-Minimal/blob/master/theme/src/main/res/drawable-nodpi/wp_lp_amber.png",
            "CC license: https://github.com/scoute-dich/Blue-Minimal/blob/master/theme/src/main/res/drawable-nodpi/wp_lp_blue.png",
            "CC license: https://github.com/scoute-dich/Blue-Minimal/blob/master/theme/src/main/res/drawable-nodpi/wp_lp_green.png",
            "CC license: https://github.com/scoute-dich/Blue-Minimal/blob/master/theme/src/main/res/drawable-nodpi/wp_lp_grey.png",
            "CC license: https://github.com/scoute-dich/Blue-Minimal/blob/master/theme/src/main/res/drawable-nodpi/wp_lp_red.png",
            "CC license: https://github.com/scoute-dich/Blue-Minimal/blob/master/theme/src/main/res/drawable-nodpi/wp_lp_teal.png",

            "CC license: https://github.com/scoute-dich/Blue-Minimal/blob/master/theme/src/main/res/drawable-nodpi/wp_mm_amber.png",
            "CC license: https://github.com/scoute-dich/Blue-Minimal/blob/master/theme/src/main/res/drawable-nodpi/wp_mm_blue.png",
            "CC license: https://github.com/scoute-dich/Blue-Minimal/blob/master/theme/src/main/res/drawable-nodpi/wp_mm_green.png",
            "CC license: https://github.com/scoute-dich/Blue-Minimal/blob/master/theme/src/main/res/drawable-nodpi/wp_mm_grey.png",
            "CC license: https://github.com/scoute-dich/Blue-Minimal/blob/master/theme/src/main/res/drawable-nodpi/wp_mm_red.png",
            "CC license: https://github.com/scoute-dich/Blue-Minimal/blob/master/theme/src/main/res/drawable-nodpi/wp_mm_teal.png",

            "CC license: https://github.com/scoute-dich/Blue-Minimal/blob/master/theme/src/main/res/drawable-nodpi/wp_n_amber.png",
            "CC license: https://github.com/scoute-dich/Blue-Minimal/blob/master/theme/src/main/res/drawable-nodpi/wp_n_blue.png",
            "CC license: https://github.com/scoute-dich/Blue-Minimal/blob/master/theme/src/main/res/drawable-nodpi/wp_n_green.png",
            "CC license: https://github.com/scoute-dich/Blue-Minimal/blob/master/theme/src/main/res/drawable-nodpi/wp_n_grey.png",
            "CC license: https://github.com/scoute-dich/Blue-Minimal/blob/master/theme/src/main/res/drawable-nodpi/wp_mm_red.png",
            "CC license: https://github.com/scoute-dich/Blue-Minimal/blob/master/theme/src/main/res/drawable-nodpi/wp_mm_teal.png", };

    final int[] itemPATH = { R.drawable.wp_lp_amber, R.drawable.wp_lp_blue, R.drawable.wp_lp_green,
            R.drawable.wp_lp_grey, R.drawable.wp_lp_red, R.drawable.wp_lp_teal,

            R.drawable.wp_mm_amber, R.drawable.wp_mm_blue, R.drawable.wp_mm_green, R.drawable.wp_mm_grey,
            R.drawable.wp_mm_red, R.drawable.wp_mm_teal,

            R.drawable.wp_n_amber, R.drawable.wp_n_blue, R.drawable.wp_n_green, R.drawable.wp_n_grey,
            R.drawable.wp_n_red, R.drawable.wp_n_teal, };

    final int[] itemPATHPREV = { R.drawable.wp_lp_amber_preview, R.drawable.wp_lp_blue_preview,
            R.drawable.wp_lp_green_preview, R.drawable.wp_lp_grey_preview, R.drawable.wp_lp_red_preview,
            R.drawable.wp_lp_teal_preview,

            R.drawable.wp_mm_amber_preview, R.drawable.wp_mm_blue_preview, R.drawable.wp_mm_green_preview,
            R.drawable.wp_mm_grey_preview, R.drawable.wp_mm_red_preview, R.drawable.wp_mm_teal_preview,

            R.drawable.wp_n_amber_preview, R.drawable.wp_n_blue_preview, R.drawable.wp_n_green_preview,
            R.drawable.wp_n_grey_preview, R.drawable.wp_n_red_preview, R.drawable.wp_n_teal_preview, };

    imgHeader = (ImageView) rootView.findViewById(R.id.imageView_header);

    if (imgHeader != null) {
        TypedArray images = getResources().obtainTypedArray(R.array.splash_images);
        int choice = (int) (Math.random() * images.length());
        imgHeader.setImageResource(images.getResourceId(choice, R.drawable.wp_lp_amber));
        images.recycle();
    }

    CustomListAdapter_Wallpaper adapter = new CustomListAdapter_Wallpaper(getActivity(), itemTITLE, itemDES,
            itemPATH, itemPATHPREV);
    ListView listView = (ListView) rootView.findViewById(R.id.bookmarks);
    listView.setAdapter(adapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // TODO Auto-generated method stub
            int Selecteditem = itemPATH[+position];
            assert imgHeader != null;
            imgHeader.setImageResource(Selecteditem);
        }
    });

    listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

            final String SelecteditemUrl = itemDES[+position].substring(12);

            Uri uri = Uri.parse(SelecteditemUrl); // missing 'http://' will cause crashed
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);

            return true;
        }
    });

    FloatingActionButton fab = (FloatingActionButton) rootView.findViewById(R.id.fab_wp);
    fab.setImageResource(R.drawable.check);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if ((imgHeader != null ? imgHeader.getDrawable() : null) == null) {
                Snackbar.make(imgHeader, R.string.check_wallpaper, Snackbar.LENGTH_LONG).show();
            } else {
                Snackbar.make(imgHeader, R.string.applying, Snackbar.LENGTH_LONG).show();

                WallpaperManager myWallpaperManager = WallpaperManager.getInstance(getActivity());
                try {
                    Bitmap bitmap = ((BitmapDrawable) imgHeader.getDrawable()).getBitmap();
                    if (bitmap != null)
                        myWallpaperManager.setBitmap(bitmap);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    });

    return rootView;
}

From source file:com.leo.runningman.ui.ImageDetailFragment.java

private void setWapaper(final Bitmap bitmap) {
    final Context mContext = this.getActivity();
    new Handler().post(new Runnable() {

        @Override//www  . j  ava  2 s  . com
        public void run() {
            try {
                WallpaperManager wpm = WallpaperManager.getInstance(mContext);
                float minH = wpm.getDesiredMinimumHeight();
                float minW = wpm.getDesiredMinimumWidth();
                Bitmap targetBitmap = getResizedBitmap(bitmap, (int) minH, (int) minW);
                wpm.setBitmap(targetBitmap);
                Toast.makeText(mContext, getActivity().getResources().getString(R.string.set_wallpaper_success),
                        200).show();
            } catch (IOException e) {
                e.printStackTrace();
                Toast.makeText(mContext, getActivity().getResources().getString(R.string.set_wallpaper_failure),
                        200).show();
            }

        }
    });
}

From source file:de.baumann.thema.FragmentWallpaper.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();

    if (id == R.id.color) {
        ColorPickerDialogBuilder.with(getActivity()).initialColor(0xff2196f3).setTitle(R.string.custom)
                .wheelType(ColorPickerView.WHEEL_TYPE.FLOWER).density(9)
                .setPositiveButton(R.string.yes, new ColorPickerClickListener() {
                    @Override//from w  ww .ja  v a 2  s .co m
                    public void onClick(DialogInterface dialog, int selectedColor, Integer[] allColors) {
                        try {
                            WallpaperManager wm = WallpaperManager.getInstance(getActivity());
                            // Create 1x1 bitmap to store the color
                            Bitmap bmp = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
                            // Make a canvas with which we can draw to the bitmap
                            Canvas canvas = new Canvas(bmp);
                            // Fill with color and save
                            canvas.drawColor(selectedColor);
                            canvas.save();

                            wm.setBitmap(bmp);
                            bmp.recycle();
                            Snackbar.make(imgHeader, R.string.applying, Snackbar.LENGTH_LONG).show();
                        } catch (IOException e) {
                            // oh lord!
                        }
                    }
                }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                }).build().show();
        return false;
    }

    if (id == R.id.help) {

        SpannableString s;

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
            s = new SpannableString(
                    Html.fromHtml(getString(R.string.help_wallpaper), Html.FROM_HTML_MODE_LEGACY));
        } else {
            //noinspection deprecation
            s = new SpannableString(Html.fromHtml(getString(R.string.help_wallpaper)));
        }
        Linkify.addLinks(s, Linkify.WEB_URLS);

        final AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity())
                .setTitle(R.string.title_wallpaper).setMessage(s)
                .setPositiveButton(getString(R.string.yes), null);
        dialog.show();

        return false;
    }
    return super.onOptionsItemSelected(item);
}

From source file:com.zertinteractive.wallpaper.activities.DetailActivity.java

@SuppressWarnings("NewApi")
@Override// w w  w. ja  v  a2s.c om
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
    case android.R.id.home:
        onBackPressed();
        return true;
    case R.id.share_image:
        shareImage();
        return true;
    case R.id.add_to_favourite:
        setFavourite();
        return true;
    case R.id.set_as_wallpaper:
        //                setAsWallpaper(imageId);

        WallpaperManager myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());
        try {
            myWallpaperManager.setBitmap(universalitmap);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return true;
    case R.id.download_image_original:
        downloadOriginalImage();
        return true;
    case R.id.downloadOriginalImage:
        downloadOriginalImage();
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}