Set Image as wallpaper using Intent

て烟熏妆下的殇ゞ 提交于 2019-12-07 18:52:05

问题


I am using the following code to set the wallpaper from an image in a drawable:

Intent setAsIntent = new Intent();
setAsIntent.setDataAndType(uri, "image/*");
setAsIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
setAsIntent.setAction("android.intent.action.ATTACH_DATA");
Intent chooserIntent = Intent.createChooser(
        setAsIntent, "set as");

But when startActivity, it only shows a message:

No apps can perform this action


回答1:


I did discover a workaround:

    Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
    intent.addCategory(Intent.CATEGORY_DEFAULT);
    intent.setDataAndType(uri, "image/jpeg");
    intent.putExtra("mimeType", "image/jpeg");
    this.startActivity(Intent.createChooser(intent, "Set as:"));

or try to use

WallpaperManager


wallpaperManager = WallpaperManager.getInstance(this);
wallpaperDrawable = wallpaperManager.getDrawable();

mImageView.setImageURI(imagepath);

If you have image bitmap then use this

wallpaperManager.setBitmap(useThisBitmap);

In your manifest file:

<uses-permission android:name="android.permission.SET_WALLPAPER"></uses-permission>


来源:https://stackoverflow.com/questions/39484906/set-image-as-wallpaper-using-intent

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