How to use getCropAndSetWallpaperIntent?

I tried using the getCropAndSetWallpaperIntent method, but got an error.

Here is my code:

Uri uri = Uri.parse("content://" + getFilesDir() + "/"+ image.path); ContentResolver contentResolver = getContentResolver(); contentResolver.getType(uri); // Type is null Intent intent = wallpaperManager.getCropAndSetWallpaperIntent(uri); intent.setType("image/*"); startActivityForResult(intent, 42); 

Here is what I got in my magazines:

 java.lang.IllegalArgumentException: Cannot use passed URI to set wallpaper; check that the type returned by ContentProvider matches image/* 

Can you help me?

+3
source share
1 answer

You should request MediaStore without using the protocol "content: //", for example, like this (the code can be improved):

 String[] paths = {"/example.png"}; final String[] FIELDS = { MediaStore.MediaColumns._ID }; // Images Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; Cursor ca = context.getContentResolver().query(uri, FIELDS, MediaStore.MediaColumns.DATA + "=?", paths, null); for (ca.moveToFirst(); !ca.isAfterLast(); ca.moveToNext()) { int id = ca.getInt(ca.getColumnIndex(MediaStore.MediaColumns._ID)); uri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id); found = true; } ca.close(); if (found) { return uri; } 
-1
source

All Articles