如何使用getCropAndSetWallpaperIntent?

时间:2014-01-13 14:44:27

标签: android android-intent uri wallpaper

我尝试使用getCropAndSetWallpaperIntent方法,但是我收到了错误。

这是我的代码:

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

以下是我在日志中的内容:

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

你能帮我吗?

1 个答案:

答案 0 :(得分:-1)

您必须在不使用“content://”协议的情况下查询MediaStore,例如这样(代码可以改进):

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