意图选择器无法设置墙纸

意图选择器无法设置墙纸

本文介绍了在Android 8(Oreo)中,意图选择器无法设置墙纸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码段设置墙纸.在低于Android 8(Oreo)的所有版本中,它都会显示一个选择器以选择锁定屏幕或主屏幕,或同时选择两者,等等.但是在Android 8中,它会直接在主屏幕上设置墙纸,而无需任何确认.是否在Oreo中进行了任何更新或代码有问题?

I'm using following code snippet to set wallpaper. In all version lower than Android 8 (Oreo) it shows a picker to choose lock screen or home screen or both etc. But in Android 8 it directly sets the wallpaper at Home screen without any confirmation. Is anything updated in Oreo or it is issue with the code?

        Uri sendUri2 = Uri.fromFile(externalFile);

        Intent intent1 = new Intent(Intent.ACTION_ATTACH_DATA);
        intent1.setDataAndType(sendUri2,type);
        intent1.putExtra("mimeType",type);
        intent1.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

        startActivityForResult(Intent.createChooser(intent1, "Set As"), 200);

推荐答案

try {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_ATTACH_DATA);
        File file = new File(path_of_file);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
        intent.setDataAndType(FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", file), getMimeType(path_of_file);
        context.startActivity(intent);
    } catch (ActivityNotFoundException e) {
        Toast.makeText(context, "Exception generated", Toast.LENGTH_SHORT).show();
    }
}


 private static String getMimeType(String url) {
    String type = null;
    String extension = MimeTypeMap.getFileExtensionFromUrl(url);
    if (extension != null) {
        type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
    }
    return type;
}

这篇关于在Android 8(Oreo)中,意图选择器无法设置墙纸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 16:32