如何使用getCropAndSetWallpaperIntent? [英] How to use getCropAndSetWallpaperIntent?

查看:483
本文介绍了如何使用getCropAndSetWallpaperIntent?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用getCropAndSetWallpaperIntent方法,但我得到了一个错误。

I tried to use getCropAndSetWallpaperIntent method but I got an error.

下面是我的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 logs :

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

你能帮助我吗?

推荐答案

您要查询MediaStore不使用的内容://协议,比如像这样(code可以改善):

You have to query the MediaStore without using the "content://" protocol, for instance like this (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;
}

这篇关于如何使用getCropAndSetWallpaperIntent?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆