Android 'set As Wallpaper' functionality

旧城冷巷雨未停 提交于 2019-11-28 01:58:24

问题


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 functionality like android default gallery:

I know it can be done by custom code (setting wallpaper and cropping image). But I want to pass that image to android default wallpaper setter so that android will manage the cropping and setting wallpaper task. How can I do that? How can I pass that image to android default walpaper setter?


回答1:


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.




回答2:


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);
}


来源:https://stackoverflow.com/questions/18778555/android-set-as-wallpaper-functionality

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!