Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
GetCropandSetWallperIntent()不适用于5.1 emulator,适用于4.4 android设备_Android_Crop_Wallpaper - Fatal编程技术网

GetCropandSetWallperIntent()不适用于5.1 emulator,适用于4.4 android设备

GetCropandSetWallperIntent()不适用于5.1 emulator,适用于4.4 android设备,android,crop,wallpaper,Android,Crop,Wallpaper,我正在尝试使用GetCropandSetWallperIntent(Uri imageUri)设置墙纸。虽然它在我的android 4.4设备上运行良好,但在我的5.1模拟器上抛出异常。例外情况是: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sobesmart.roseswallpaper/com.sobesmart.roseswallpaper.DetailAc tivity}: java.la

我正在尝试使用GetCropandSetWallperIntent(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();
    }
}
我检查了方法getcropandsetwallperintent(uriimageuri)的定义。最后一行是:

// 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/*");
  • Uri不是正确的类型:类Uri没有getType()方法。并且没有类型字段。如何检查Uri的类型
  • 出于某种原因,系统壁纸裁剪器不存在:是否有人知道是否存在用于模拟器的裁剪器。在调用此方法之前,是否有方法检查特定设备是否存在裁剪器