Usman Rana
Usman Rana

Reputation: 2142

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

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

Upvotes: 1

Views: 552

Answers (2)

Armin Haghighi
Armin Haghighi

Reputation: 66

   Intent setWith = new Intent(Intent.ACTION_ATTACH_DATA);
                    setWith.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    setWith.putExtra("SET_LOCKSCREEN_WALLPAPER", true);
                    setWith.addCategory(Intent.CATEGORY_DEFAULT);
                    setWith.setDataAndType(Uri.parse("file://" + imageFile.getAbsolutePath()), "image/*");
                    setWith.putExtra("mimeType", "image/*");
                   context.startActivity(Intent.createChooser(setWith, "Set As"));

Upvotes: 0

QUIBO
QUIBO

Reputation: 21

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

Upvotes: 2

Related Questions