Unable to set picture as. “No appps can perform this action”

假装没事ソ 提交于 2019-12-24 01:06:58

问题


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.


回答1:


Android file read format has been changed after targetSdkVersion >= 24

You can find the details here; https://stackoverflow.com/a/38858040/1367450



来源:https://stackoverflow.com/questions/35619631/unable-to-set-picture-as-no-appps-can-perform-this-action

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