In Android 8 (Oreo) Intent Chooser is not working to set WallPaper

大城市里の小女人 提交于 2019-12-11 13:37:18

问题


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

回答1:


esa es mi solucion

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


来源:https://stackoverflow.com/questions/51546617/in-android-8-oreo-intent-chooser-is-not-working-to-set-wallpaper

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