直接从我们的android应用(例如zedge)将图像设置为主屏幕或锁定屏幕的背景

问题描述 投票:-1回答:1

我正在开发具有许多壁纸的android应用。我添加了下载图像的选项,并且工作正常。但是现在我想添加将图像直接设置为主屏幕背景或锁定屏幕背景的选项。这是我下载图片的代码

  buttonDownload.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ((Activity) Picture.this).findViewById(R.id.progressbar_open_pic).setVisibility(View.VISIBLE);

            Glide.with(Picture.this)
                    .asBitmap()
                    .load(url)
                    .into(new SimpleTarget<Bitmap>() {
                              @Override
                              public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
                                  ((Activity) Picture.this).findViewById(R.id.progressbar_open_pic).setVisibility(View.GONE);

                                  Intent intent = new Intent(Intent.ACTION_VIEW);

                                  Uri uri = saveWallpaperAndGetUri(resource, id);

                                  if (uri != null) {
                                      intent.setDataAndType(uri, "image/*");
                                      Picture.this.startActivity(Intent.createChooser(intent, "Wallpaperized"));
                                  }
                              }
                          }
                    );

        }
    });

  private Uri saveWallpaperAndGetUri(Bitmap bitmap, String id) {
        if (ContextCompat.checkSelfPermission(Picture.this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {

            if (ActivityCompat
                    .shouldShowRequestPermissionRationale((Activity) Picture.this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)) {

                Intent intent = new Intent();
                intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);

                Uri uri = Uri.fromParts("package", Picture.this.getPackageName(), null);
                intent.setData(uri);

                Picture.this.startActivity(intent);

            } else {
                ActivityCompat.requestPermissions((Activity) Picture.this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 100);
            }
            return null;
        }

        File folder = new File(Environment.getExternalStorageDirectory().toString() + "/Download");
        folder.mkdirs();

        File file = new File(folder, id + ".jpg");
        try {
            FileOutputStream out = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
            out.flush();
            out.close();
            Toast.makeText(Picture.this, "Image has been saved", Toast.LENGTH_SHORT).show();
            Picture.this.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));
            return FileProvider.getUriForFile(Picture.this, BuildConfig.APPLICATION_ID + ".provider", file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

请向我建议一些将图像直接设置为桌面背景的方法,并且该图像应像zedge一样保存在移动存储中

android
1个回答
0
投票

InputStream ins = null;

                                try {
                                    WallpaperManager myWallpaperManager
                                            = WallpaperManager.getInstance(getActivity());

                                    ins = new URL(url).openStream();
                                    myWallpaperManager.setStream(ins);
                                    Toast.makeText(getActivity(), "set as Wallpaper", Toast.LENGTH_SHORT).show();
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
© www.soinside.com 2019 - 2024. All rights reserved.