Giter VIP home page Giter VIP logo

setwallpaper's Introduction

兼容国产系统的设置壁纸方式

废话不多说,主要代码:

public static void intent2SetWallPaper(Context context, String path) {
        Uri uriPath = getUriWithPath(path);
        Intent intent;

        // 针对EMUI
        if (RomUtil.isHuaweiRom()) {
            try {
                ComponentName componentName = new ComponentName("com.android.gallery3d", "com.android.gallery3d.app.Wallpaper");
                intent = new Intent(Intent.ACTION_VIEW);
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                intent.setDataAndType(uriPath, "image/*");
                intent.putExtra("mimeType", "image/*");
                intent.setComponent(componentName);
                context.startActivity(intent);
            } catch (Exception e) {
                e.printStackTrace();
                try {
                    WallpaperManager.getInstance(context.getApplicationContext()).setBitmap(ImageUtil.getImageBitmap(path));
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }

        // 针对MIUI
        } else if (RomUtil.isMiuiRom()) {
            try {
                ComponentName componentName = new ComponentName("com.android.thememanager", "com.android.thememanager.activity.WallpaperDetailActivity");
                intent = new Intent("miui.intent.action.START_WALLPAPER_DETAIL");
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                intent.setDataAndType(uriPath, "image/*");
                intent.putExtra("mimeType", "image/*");
                intent.setComponent(componentName);
                context.startActivity(intent);
            } catch (Exception e) {
                e.printStackTrace();
                try {
                    WallpaperManager.getInstance(context.getApplicationContext()).setBitmap(ImageUtil.getImageBitmap(path));
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }

        // 其他
        } else {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                try {
                    intent =
                        WallpaperManager.getInstance(context.getApplicationContext()).getCropAndSetWallpaperIntent(uriPath);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.getApplicationContext().startActivity(intent);
                } catch (IllegalArgumentException e) {
                    Bitmap bitmap = null;
                    try {
                        bitmap = MediaStore.Images.Media.getBitmap(context.getApplicationContext().getContentResolver(), uriPath);
                        if (bitmap != null) {
                            WallpaperManager.getInstance(context.getApplicationContext()).setBitmap(bitmap);
                        }
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
            } else {
                try {
                    WallpaperManager.getInstance(context.getApplicationContext()).setBitmap(ImageUtil.getImageBitmap(path));
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }

为方便大家使用,封装到了github: https://github.com/SherlockGougou/SetWallpaper

使用方式:

1.添加依赖:

Step 1. 在你project层级的build.gradle中,添加仓库地址:
allprojects {
	repositories {
		...
		maven { url 'https://jitpack.io' }
	}
}
Step 2. 在你主module的build.gradle中添加依赖:
dependencies {
	 implementation 'com.github.SherlockGougou:SetWallpaper:v1.3.0'
}

2.调用代码:

SetWallpaper.setWallpaper(MainActivity.this, // 上下文
                path, // 图片绝对路径
                APP_AUTHORITY);// authority(7.0 文件共享权限)

setwallpaper's People

Contributors

sherlockgougou avatar lovexyx avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.