Example usage for android.app WallpaperManager setStream

List of usage examples for android.app WallpaperManager setStream

Introduction

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

Prototype

@RequiresPermission(android.Manifest.permission.SET_WALLPAPER)
public void setStream(InputStream bitmapData) throws IOException 

Source Link

Document

Change the current system wallpaper to a specific byte stream.

Usage

From source file:com.cuddlesoft.nori.fragment.ImageFragment.java

/**
 * Downloads the full-resolution image in the background and sets it as the wallpaper.
 *//*from  w  w  w . ja  v a 2  s . com*/
protected void setAsWallpaper() {
    // Fetch and set full-screen image as wallpaper on background thread.
    final Context context = getActivity();
    final String imageUrl = image.fileUrl;
    final WallpaperManager wallpaperManager = WallpaperManager.getInstance(getActivity());

    new AsyncTask<Void, Void, Exception>() {
        @Override
        protected Exception doInBackground(Void... ignored) {
            try {
                InputStream inputStream = new URL(imageUrl).openStream();
                wallpaperManager.setStream(inputStream);
            } catch (IOException e) {
                return e;
            }
            return null;
        }

        @Override
        protected void onPostExecute(Exception error) {
            if (error != null) {
                // Show error message to the user.
                Toast.makeText(context, String.format(context.getString(R.string.toast_couldNotSetWallpaper),
                        error.getLocalizedMessage()), Toast.LENGTH_LONG).show();
            }
        }
    }.execute();
}

From source file:com.mklodoss.SexyGirl.displayingbitmaps.ui.ImageDetailActivity.java

/**
 * ?/* w w w  .j av a2  s  . c o  m*/
 */
private void setWallpaper() {
    position = mPager.getCurrentItem();
    new Thread() {
        @Override
        public void run() {
            if (ImageGridFragment.list != null && ImageGridFragment.list.size() > position) {
                LocalBelle belle = ImageGridFragment.list.get(position);
                String rawUrl = belle.getRawUrl();
                try {
                    File localFile = new File(AppRuntime.RAW_URL_CACHE_DIR + rawUrl.hashCode());
                    if ((localFile != null) && (localFile.exists())) {
                        WallpaperManager localWallpaperManager = WallpaperManager
                                .getInstance(ImageDetailActivity.this);
                        try {
                            localWallpaperManager.setStream(new FileInputStream(localFile));
                            ImageDetailActivity.this.mHandler.sendEmptyMessage(WHAT_WALLPAPER_SUCCESS);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    } else {
                        ImageDetailActivity.this.mHandler.sendEmptyMessage(WHAT_WALLPAPER_FAIL);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    ImageDetailActivity.this.mHandler.sendEmptyMessage(WHAT_WALLPAPER_FAIL);
                }
            }
        }
    }.start();
}

From source file:io.github.tjg1.nori.fragment.ImageFragment.java

/**
 * Downloads the full-resolution image in the background and sets it as the wallpaper.
 *//*from w  w w.j  ava  2 s  . co  m*/
protected void setAsWallpaper() {
    // Fetch and set full-screen image as wallpaper on background thread.
    final Context context = getContext();
    final String imageUrl = image.fileUrl;
    final WallpaperManager wallpaperManager = WallpaperManager.getInstance(getContext());

    new AsyncTask<Void, Void, Exception>() {
        @Override
        protected Exception doInBackground(Void... ignored) {
            try {
                InputStream inputStream = new URL(imageUrl).openStream();
                wallpaperManager.setStream(inputStream);
            } catch (IOException e) {
                return e;
            }
            return null;
        }

        @Override
        protected void onPostExecute(Exception error) {
            if (error != null) {
                // Show error message to the user.
                View view = getView();
                if (view != null) {
                    Snackbar.make(view, String.format(context.getString(R.string.toast_couldNotSetWallpaper),
                            error.getLocalizedMessage()), Snackbar.LENGTH_LONG).show();
                }
            }
        }
    }.execute();
}

From source file:com.gmail.charleszq.picorner.ui.ImageDetailFragment.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if (mLoadedBitmap != null) {
        saveBitmapToShare(mLoadedBitmap, null);
    }//from  w  w w  .ja  va2s  .  c  o m

    switch (item.getItemId()) {
    case android.R.id.home:
        getActivity().finish();
        return true;
    case R.id.menu_item_share_action_provider_action_bar:
        boolean ready = waitForImageLoaded();
        if (ready) {
            sharePhoto();
        }
        return ready;
    case R.id.menu_item_slide_show:
        ImageDetailActivity act = (ImageDetailActivity) getActivity();
        if (act != null) {
            act.startSlideShow(0);
        }
        return true;
    case R.id.menu_item_see_owner_photos:
        Intent i = new Intent(getActivity(), UserPhotoListActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        i.putExtra(UserPhotoListActivity.MD_TYPE_KEY, mPhoto.getMediaSource().ordinal());
        i.putExtra(UserPhotoListActivity.USER_KEY, mPhoto.getAuthor());
        startActivity(i);
        return true;
    case R.id.menu_item_like:
        ready = waitForImageLoaded();
        if (ready)
            likePhoto(item);
        return ready;
    case R.id.menu_item_vote:
        ready = waitForImageLoaded();
        if (ready)
            votePhoto();
        return ready;
    case R.id.menu_item_save:
        ready = waitForImageLoaded();
        if (ready)
            savePhotoLocally();
        return ready;
    case R.id.menu_item_set_wallpaper:
        ready = waitForImageLoaded();
        if (!ready)
            return false;

        WallpaperManager wm = WallpaperManager.getInstance(getActivity());
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(getShareImageFile(null));
            wm.setStream(fis);
            Toast.makeText(getActivity(), getResources().getString(R.string.msg_wallpaper_changed),
                    Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                }
            }
        }
        return true;
    case R.id.menu_item_detail:
        showPhotoDetailWithPage(null);
        return true;
    case R.id.menu_item_comment:
        showPhotoDetailWithPage(PhotoDetailActivity.COMMENT_PAGE);
        return true;
    case R.id.menu_item_photo_set:
        showPhotoDetailWithPage(PhotoDetailActivity.MY_F_ORG_PHOTO_SET_PAGE);
        return true;
    case R.id.menu_item_add_to_group:
        showPhotoDetailWithPage(PhotoDetailActivity.MY_F_ORG_GROUP_PAGE);
        return true;
    case R.id.menu_item_view_exif:
        showPhotoDetailWithPage(PhotoDetailActivity.EXIF_PAGE);
        return true;
    case R.id.menu_item_view_on_map:
        showPhotoDetailWithPage(PhotoDetailActivity.MAP_PAGE);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

From source file:ooo.oxo.mr.ViewerActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    binding = DataBindingUtil.setContentView(this, R.layout.viewer_activity);

    setTitle(null);/*  w ww .  j  a va 2 s  .com*/

    binding.toolbar.setNavigationOnClickListener(v -> supportFinishAfterTransition());
    binding.toolbar.inflateMenu(R.menu.viewer);

    binding.puller.setCallback(this);

    supportPostponeEnterTransition();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        getWindow().getEnterTransition().addListener(new SimpleTransitionListener() {
            @Override
            @TargetApi(Build.VERSION_CODES.LOLLIPOP)
            public void onTransitionEnd(Transition transition) {
                getWindow().getEnterTransition().removeListener(this);
                fadeIn();
            }
        });
    } else {
        fadeIn();
    }

    background = new ColorDrawable(Color.BLACK);
    binding.getRoot().setBackground(background);

    adapter = new Adapter();

    binding.pager.setAdapter(adapter);
    binding.pager.setCurrentItem(getIntent().getIntExtra("index", 0));
    binding.pager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageScrollStateChanged(int state) {
            if (state == ViewPager.SCROLL_STATE_DRAGGING) {
                fadeOut();
            }
        }
    });

    listener = new ObservableListPagerAdapterCallback(adapter);
    images.addOnListChangedCallback(listener);

    setEnterSharedElementCallback(new SharedElementCallback() {
        @Override
        public void onMapSharedElements(List<String> names, Map<String, View> sharedElements) {
            Image image = images.get(binding.pager.getCurrentItem());
            sharedElements.clear();
            sharedElements.put(String.format("%s.image", image.getObjectId()), getCurrent().getSharedElement());
        }
    });

    menuItemClicks(R.id.share).compose(bindToLifecycle())
            .compose(ensurePermissions(Manifest.permission.WRITE_EXTERNAL_STORAGE))
            .map(avoid -> getCurrentImage())
            .doOnNext(image -> MobclickAgent.onEvent(this, "share", image.getObjectId()))
            .observeOn(Schedulers.io()).flatMap(this::saveIfNeeded).observeOn(AndroidSchedulers.mainThread())
            .doOnNext(this::notifyMediaScanning).map(Uri::fromFile).retry().subscribe(uri -> {
                final Intent intent = new Intent(Intent.ACTION_SEND);
                intent.setType("image/jpeg");
                intent.putExtra(Intent.EXTRA_STREAM, uri);
                startActivity(Intent.createChooser(intent, getString(R.string.share_title)));
            });

    menuItemClicks(R.id.save).compose(bindToLifecycle())
            .compose(ensurePermissions(Manifest.permission.WRITE_EXTERNAL_STORAGE))
            .map(avoid -> getCurrentImage())
            .doOnNext(image -> MobclickAgent.onEvent(this, "save", image.getObjectId()))
            .observeOn(Schedulers.io()).flatMap(this::saveIfNeeded).observeOn(AndroidSchedulers.mainThread())
            .doOnNext(this::notifyMediaScanning).retry().subscribe(file -> {
                ToastUtil.shorts(this, R.string.save_success, file.getPath());
            });

    final WallpaperManager wm = WallpaperManager.getInstance(this);

    menuItemClicks(R.id.set_wallpaper).compose(bindToLifecycle()).map(avoid -> getCurrentImage())
            .doOnNext(image -> MobclickAgent.onEvent(this, "set_wallpaper", image.getObjectId()))
            .observeOn(Schedulers.io()).flatMap(this::download).observeOn(AndroidSchedulers.mainThread())
            .map(file -> FileProvider.getUriForFile(this, AUTHORITY_IMAGES, file)).retry().subscribe(uri -> {
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
                    startActivity(wm.getCropAndSetWallpaperIntent(uri));
                } else {
                    try {
                        wm.setStream(getContentResolver().openInputStream(uri));
                        ToastUtil.shorts(this, R.string.set_wallpaper_success);
                    } catch (IOException e) {
                        Log.e(TAG, "Failed to set wallpaper", e);
                        ToastUtil.shorts(this, e.getMessage(), e);
                    }
                }
            });
}