Unable to set picture as. “No appps can perform th

2019-08-12 08:11发布

问题:

I am trying to give users an option to set image as wallpaper/whatsapp dp like this.

But I'm stuck with this code

Uri sendUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.a_day_without_thinking_mobile);

Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
intent.setDataAndType(sendUri, "image/jpg");
intent.putExtra("mimeType", "image/jpg");
startActivity(Intent.createChooser(intent,"Set As"));

It shows a dialog that no apps can perform this action. I also tried to check my Uri through this method

        ContentResolver cr = getContentResolver();
        String[] projection = {MediaStore.MediaColumns.DATA};
        Cursor cur = cr.query(sendUri, projection, null, null, null);
        if (cur != null) {
            if (cur.moveToFirst()) {
                String filePath = cur.getString(0);

                if (new File(filePath).exists()) {
                    Log.d("URI: ","File path exist");
                } else {
                    Log.d("URI: ","File not found");
                }
            } else {
                Log.d("URI: ","URI ok but no enty found");
            }
            cur.close();
        } else {
            Log.d("URI: ","URI was invalid for some other reason");
        }

And It always returned that the URI was invalid. But I'm sure that the image is valid jpg and is present in raw folder. I tried changing URI paths but to no success.