设置wallpater时出错:将壁纸管理器从中心裁剪图像,然后再将其设置为墙纸

时间:2019-05-28 09:13:46

标签: java android

想要设置墙纸而不会裁剪

       StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);

            WallpaperManager wpm = WallpaperManager.getInstance(getApplicationContext());
            InputStream ins = null;
            try {
              if(!getFile(Environment.getExternalStorageDirectory()).get(0).exists()) {
                  Uri uri = Uri.parse(Imageurl);

为Android下载管理器创建请求

        DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
                  DownloadManager.Request request = new DownloadManager.Request(uri);
                  request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI |
                          DownloadManager.Request.NETWORK_MOBILE);

                  request.setTitle("Data Download");
                  request.setDescription("Android Data download using DownloadManager.");

                  request.allowScanningByMediaScanner();
                  request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

将下载文件的本地目标设置为应用程序外部文件目录中的路径

         request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "Image.png");
                  request.setMimeType("*/*");
                  downloadManager.enqueue(request);
              }else {
                  DisplayMetrics metrics = new DisplayMetrics();
                  getWindowManager().getDefaultDisplay().getMetrics(metrics);
                  int height = metrics.heightPixels;
                  int width = metrics.widthPixels;
                  BitmapFactory.Options bmOptions = new BitmapFactory.Options();
                  Bitmap bitmap = BitmapFactory.decodeFile(getFile(Environment.getExternalStorageDirectory()).get(0).getAbsolutePath(),bmOptions);
                  bitmap = Bitmap.createScaledBitmap(bitmap,width,height,true);
                  wpm.setBitmap(bitmap);
                  wpm.suggestDesiredDimensions(350, 500);

0 个答案:

没有答案