private void onSetAsWallpaper() { WallpaperManager wallpaperManager = WallpaperManager.getInstance(this); if (wallpaperFile == null || !wallpaperFile.exists()) { Snackbar.make(rootLayout, R.string.fab_snack_bar_image_not_cached, Snackbar.LENGTH_LONG) .show(); } else { if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) { Bitmap wallpaper = BitmapFactory.decodeFile(wallpaperFile.getAbsolutePath()); if (wallpaper != null) { try { wallpaperManager.setBitmap(wallpaper); if (floatingActionMenu.isOpened()) floatingActionMenu.close(true); Toast.makeText(this, "Image set as Wallpaper", Toast.LENGTH_SHORT).show(); } catch (IOException io) { io.printStackTrace(); } } } else { Uri cropWallpaperUri = FileProvider.getUriForFile(this, "com.pddstudio.fileprovider", wallpaperFile); if (cropWallpaperUri != null) { Intent cropIntent = wallpaperManager.getCropAndSetWallpaperIntent(cropWallpaperUri); startActivity(cropIntent); } } } }
@Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.btn_apply: try { wallpaperManager.setBitmap(mWall.getDrawingCache()); } catch (IOException e) { e.printStackTrace(); } break; } }
@Override public void run() { WallpaperManager wm = WallpaperManager.getInstance(mContext); Drawable oldWallpaper = wm.getDrawable(); InputStream inputstream = null; try { // TODO: This will cause the resource to be downloaded again, when // we should in most cases be able to grab it from the cache. To fix // this we should query WebCore to see if we can access a cached // version and instead open an input stream on that. This pattern // could also be used in the download manager where the same problem // exists. inputstream = openStream(); if (inputstream != null) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); int seg = 0; byte buf[] = new byte[8192]; while ((seg = inputstream.read(buf)) != -1) { baos.write(buf, 0, seg); } final byte[] imageData = baos.toByteArray(); BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; // We give decodeStream a wrapped input stream so it doesn't // mess with our mark (currently it sets a mark of 1024) Bitmap firstImage = BitmapFactory.decodeByteArray(imageData, 0, imageData.length, options); int maxWidth = wm.getDesiredMinimumWidth(); int maxHeight = wm.getDesiredMinimumHeight(); // Give maxWidth and maxHeight some leeway maxWidth *= 1.25; maxHeight *= 1.25; int bmWidth = options.outWidth; int bmHeight = options.outHeight; int scale = 1; while (bmWidth > maxWidth || bmHeight > maxHeight) { scale <<= 1; bmWidth >>= 1; bmHeight >>= 1; } options.inJustDecodeBounds = false; options.inSampleSize = scale; Bitmap scaledWallpaper = BitmapFactory.decodeByteArray(imageData, 0, imageData.length, options); if (scaledWallpaper != null) { wm.setBitmap(scaledWallpaper); } } } catch (IOException e) { Log.e(LOGTAG, "Unable to set new wallpaper"); // Act as though the user canceled the operation so we try to // restore the old wallpaper. mCanceled = true; } finally { if (inputstream != null) { try { inputstream.close(); } catch (IOException e) { // Ignore } } } if (mCanceled) { // Restore the old wallpaper if the user cancelled whilst we were // setting // the new wallpaper. int width = oldWallpaper.getIntrinsicWidth(); int height = oldWallpaper.getIntrinsicHeight(); Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); Canvas canvas = new Canvas(bm); oldWallpaper.setBounds(0, 0, width, height); oldWallpaper.draw(canvas); canvas.setBitmap(null); try { wm.setBitmap(bm); } catch (IOException e) { Log.e(LOGTAG, "Unable to restore old wallpaper."); } mCanceled = false; } if (mWallpaperProgress.isShowing()) { mWallpaperProgress.dismiss(); } }