我正在尝试使用getCropAndSetWallpaperIntent(Uri imageUri)来设置壁纸。虽然它在我的Android 4.4设备上工作正常,它会在我的5.1模拟器上抛出异常。例外是:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sobesmart.roseswallpaper/com.sobesmart.roseswallpaper.DetailAc
tivity}: java.lang.IllegalArgumentException: Cannot use passed URI to set
wallpaper; check that the type returned by ContentProvider matches image/*
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
Caused by: java.lang.IllegalArgumentException: Cannot use passed URI to set wallpaper; check that the type returned by ContentProvider matches image/*
at android.app.WallpaperManager.getCropAndSetWallpaperIntent(WallpaperManager.java:894)
at
com.sobesmart.roseswallpaper.DetailActivity.onCreate(DetailActivity.java:81)
虽然对此有很多疑问,但所有这些都指向了我也使用的相同答案。这是我的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
try{
//Pick Image From Gallery
Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_SELECT_IMAGE);
}catch(Exception e){
e.printStackTrace();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RESULT_SELECT_IMAGE:
if (resultCode == Activity.RESULT_OK && data != null && data.getData() != null) {
try {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
CropImage(picturePath);
//perform Crop on the Image Selected from Gallery
} catch (Exception e) {
e.printStackTrace();
finish();
}
} else {
Log.i(TAG, "RESULT_CANCELED");
finish();
}
break;
case RESULT_CROP:
if (resultCode == Activity.RESULT_OK) {
//wallpaper is set
finish();
}
else
finish();
break;
}
}
private void CropImage(String picUri)
{
WallpaperManager wallpaperManager = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ECLAIR) {
wallpaperManager = WallpaperManager.getInstance(FullscreenActivity.this);
}
Uri contentUri = getImageContentUri(this,picUri);
Intent intent = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
intent = wallpaperManager.getCropAndSetWallpaperIntent(contentUri);
}
startActivityForResult(intent,RESULT_CROP);
}
public static Uri getImageContentUri(Context context, String absPath) {
Log.v(TAG, "getImageContentUri: " + absPath);
Cursor cursor = context.getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI
, new String[] { MediaStore.Images.Media._ID }
, MediaStore.Images.Media.DATA + "=? "
, new String[] { absPath }, null);
if (cursor != null && cursor.moveToFirst()) {
int id = cursor.getInt(cursor.getColumnIndex(MediaStore.MediaColumns._ID));
return Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI , Integer.toString(id));
} else if (!absPath.isEmpty()) {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DATA, absPath);
return context.getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
} else {
return null;
}
}
private void performCrop(String picUri) {
try {
//Start Crop Activity
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
// get the height and width of screen
height = metrics.heightPixels;
width = metrics.widthPixels ;
Intent cropIntent = new Intent("com.android.camera.action.CROP");
// indicate image type and Uri
File f = new File(picUri);
Uri contentUri = Uri.fromFile(f);
cropIntent.setDataAndType(contentUri, "image/*");
// set crop properties
cropIntent.putExtra("crop", "true");
// indicate aspect of desired crop
cropIntent.putExtra("aspectX", width);
cropIntent.putExtra("aspectY", height);
// indicate output X and Y
cropIntent.putExtra("outputX", width);
cropIntent.putExtra("outputY", height);
// retrieve data on return
cropIntent.putExtra("return-data", true);
// start the activity - we handle returning in onActivityResult
startActivityForResult(cropIntent, RESULT_CROP);
}
// respond to users whose devices do not support the crop action
catch (ActivityNotFoundException anfe) {
// display an error message
String errorMessage = "your device doesn't support the crop action!";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}
我检查了方法getCropAndSetWallpaperIntent(Uri imageUri)的定义。它的最后一行是:
// If the URI is not of the right type, or for some reason the system wallpaper
// cropper doesn't exist, return null
throw new IllegalArgumentException("Cannot use passed URI to set wallpaper; " +
"check that the type returned by ContentProvider matches image/*");