In Android 8 (Oreo) Intent Chooser is not working

2019-07-29 04:40发布

问题:

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