Android 代码设置壁纸,兼容华为 小米 OPPO Vivo
2020-03-31 本文已影响0人
远在远方的风yzyfdf
华为
/**
* 华为
*/
private static void setHuawei(Context context, String path, Uri uriPath) {
Intent intent;
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();
setWallpaperDefault(context, path);
}
}
小米
/**
* 小米
*/
private static void setXiaomi(Context context, String path, Uri uriPath) {
Intent intent;
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();
setWallpaperDefault(context, path);
}
}
OPPO
/**
* Vivo
*/
private static void setVivo(Context context, String path, Uri uriPath) {
Intent intent;
try {
ComponentName componentName =
new ComponentName("com.vivo.gallery", "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();
setWallpaperDefault(context, path);
}
}
Vivo
/**
* OPPO
*/
private static void setOppo(Context context, String path, Uri uriPath) {
Intent intent;
try {
ComponentName componentName =
new ComponentName("com.coloros.gallery3d", "com.oppo.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();
setWallpaperDefault(context, path);
}
}
其他
/**
* 其他
*/
private static void setOthers(Context context, String path, File file) {
try {
context.startActivity(WallpaperManager.getInstance(context.getApplicationContext())
.getCropAndSetWallpaperIntent(getImageContentUri(context, file)));
} catch (Exception e) {
e.printStackTrace();
setWallpaperDefault(context, path);
}
}
/**
* 默认方法
*/
private static void setWallpaperDefault(Context context, String path) {
try {
WallpaperManager.getInstance(context.getApplicationContext()).setBitmap(ImageUtils.getBitmap(path));
} catch (IOException e) {
e.printStackTrace();
}
}
判断厂商的工具类 推荐 Blankj/AndroidUtilCode
申请存储权限就自己写吧