gpt4 book ai didi

android - 如何使用getCropAndSetWallpaperIntent?

转载 作者:太空狗 更新时间:2023-10-29 15:26:53 27 4
gpt4 key购买 nike

我尝试使用 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/*

你能帮帮我吗?

最佳答案

你必须在不使用“content://”协议(protocol)的情况下查询 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;
}

关于android - 如何使用getCropAndSetWallpaperIntent?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21094023/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com