2

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

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.

Jack Stern
  • 395
  • 1
  • 4
  • 16
  • What problem you are facing? – Swaminathan V Feb 25 '16 at 17:03
  • When I select the option it shows "No apps can perform this action". I think maybe it is due to proper image URI not loading. – Jack Stern Feb 25 '16 at 18:47
  • Actually do you want to attach / post the file to the social media. is this correct ? – Swaminathan V Feb 26 '16 at 05:19
  • No, I want to give options to users to set the image as wallpaper. As shown in the above pic. But that dialog Isn't popping up Instead it is showing me a message that 'no apps can perform this action'. – Jack Stern Feb 26 '16 at 10:05
  • so From your app, you are capturing the picture and you want that picture to be used to kept as DP for social medias as mentioned above – Swaminathan V Feb 26 '16 at 10:31
  • Image is already present inside the raw folder. I want to use that image which can be used to be set as wallpaper/dp. Kind of a wallpaper app only the images will not be on the server but inside the raw folder. – Jack Stern Feb 26 '16 at 10:39
  • Not sure if there is an option to set wallpaper from the app. Mostly Intent are used to share the images to the respective medias. – Swaminathan V Feb 26 '16 at 10:57
  • 1
    But then how does wallpaper apps works. They also provide an option to set an image as wallpaper from within the app. – Jack Stern Feb 26 '16 at 12:22

1 Answers1

0

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

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

Sabri Meviş
  • 2,231
  • 1
  • 32
  • 38