Android 'set As Wallpaper' functionality

后端 未结 2 1010
广开言路
广开言路 2020-12-18 14:51

I am developing an application in which I have an image gallery and when I click on any image, it opens in full mode. But I want the set As Wallpaper functional

相关标签:
2条回答
  • 2020-12-18 15:25

    This is my code which downloads a image from a URL.You can find it useful.Don't forget to add the required permissions for storage,wallpaper and internet.

                 @Override
    public void onClick(View v) {
        setWall(v);
      }
     });
    
     if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
    
            // Should we show an explanation?
            if (shouldShowRequestPermissionRationale(
                    Manifest.permission.READ_EXTERNAL_STORAGE)) {
                // Explain to the user why we need to read the contacts
            }
    
            requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                    MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
    
            // MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE is an
            // app-defined int constant that should be quite unique
    
            return;
        }
    }
    
    
    
    
    public void setWall(View view) {
    
        new SetWallpaperTask().execute();
    }
    
    public class SetWallpaperTask extends AsyncTask <String, Void, Bitmap> {
        String image = getIntent().getStringExtra("image");
        ProgressDialog progressDialog;
        @TargetApi(Build.VERSION_CODES.KITKAT)
        @Override
        protected Bitmap doInBackground(String... params) {
            Bitmap result= null;
            try {
                result = Picasso.with(getApplicationContext())
                        .load(image)
                        .get();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            WallpaperManager wallpaperManager = WallpaperManager.getInstance(getApplicationContext());
    
    
    
            //new Intent(wallpaperManager.getCropAndSetWallpaperIntent(getImageUri(result,getApplicationContext())));
    
    
            return result;
        }
    
    
    
        @TargetApi(Build.VERSION_CODES.KITKAT)
        @Override
        protected void onPostExecute (Bitmap result) {
            super.onPostExecute(result);
    
            WallpaperManager wallpaperManager = WallpaperManager.getInstance(getBaseContext());
          {
              startActivity(new Intent(wallpaperManager.getCropAndSetWallpaperIntent(getImageUri(result,getApplicationContext()))));
    
              //  wallpaperManager.setBitmap(result);
    
                progressDialog.dismiss();
              //  Toast.makeText(getApplicationContext(), "Set wallpaper successfully", Toast.LENGTH_SHORT).show();
    
        }}
    
        @Override
        protected void onPreExecute () {
            super.onPreExecute();
    
            progressDialog = new ProgressDialog(Wallpaper_activity.this);
            progressDialog.setMessage("Please wait...");
            progressDialog.setCancelable(false);
            progressDialog.show();
        }
    }
    private Uri getImageUri(Bitmap inImage, Context inContext) {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
        String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(),
                inImage, "Title", null);
        return Uri.parse(path);
    }
    
    0 讨论(0)
  • 2020-12-18 15:42

    You can launch Crop intent by start activity for result and retrieve it in result and then use wallpaper manager class. like this

    Uri imgUri=Uri.parse("android.resource://your.package.name/"+R.drawable.image); 
    Intent intent = new Intent("com.android.camera.action.CROP");  
    intent.setDataAndType(imgUri, "image/*");  
    intent.putExtra("crop", "true");  
    intent.putExtra("aspectX", 1);  
    intent.putExtra("aspectY", 1);  
    intent.putExtra("outputX", 80);  
    intent.putExtra("outputY", 80);  
    intent.putExtra("return-data", true);
    startActivityForResult(intent, REQUEST_CODE_CROP_PHOTO);
    

    and use Wallpaper manager in your onResult function

    Also keep in mind that It depends on the device whether that device is support it or not. This Intent action is not part of the internal API. Some manufacturers provide their own Gallery apps and so there is no way of knowing whether or not the user's device will recognize the Intent.

    0 讨论(0)
提交回复
热议问题