2014-01-13 36 views
3

我試圖使用getCropAndSetWallpaperIntent方法,但我得到一個錯誤。如何使用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/* 

你能幫助我嗎?

+0

弄來的解決方案? – user4057066

回答

-1

你要查詢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; 
} 
+0

仍然是同樣的錯誤。它返回內容:// media/external/images/media/63,其中文件的路徑爲「/ storage/emulated/0/Pictures/catamaran-199153_1920.jpg」。此值傳遞給getCropAndSetWallpaperIntent()時拋出異常:java.lang.IllegalArgumentException:無法使用傳遞的URI來設置壁紙;檢查由ContentProvider返回的類型是否匹配image/* – user4057066