裁剪图像并将其设置为背景

时间:2014-08-14 07:00:29

标签: android background

每当我从图库中选择一张图片将其设置为背景时,它会缩小(如果尺寸超出设备分辨率)。因此,在将其设置为背景之前,我会裁剪所选图像。但不知何故,它不会在裁剪后将其设置为背景。我知道这一定是一个愚蠢的错误,但我现在无法解决这个问题。

有人可以指出问题是什么吗? 感谢。

    private void wallp() // For changing wallpaper
    {
        Intent intent = new Intent();
        intent.setType("image/*");

        startActivityForResult(Intent.createChooser(intent, "Select Picture"),
                SELECT_PICTURE);

    }
    private void crop(Uri photoUri) {
        Intent intent = new Intent();
        intent.setData(photoUri);
        intent.putExtra("outputX", 1256);
        intent.putExtra("outputY", 720);
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        intent.putExtra("scale", true);
        intent.putExtra("return-data", true);

        startActivityForResult(intent, RESULT_CROP);
    }

ActivityForResult

case SELECT_PICTURE:
            if (resultcode == RESULT_OK) {
                  Uri photoUri = data.getData();
                    if (photoUri != null) {
                        crop(photoUri);
                    }
                } else if (resultcode == RESULT_CROP) {
                    Uri myimage = data.getData();

                    File f = new File(getIntent().getExtras().containsKey(key));
                    if (f.exists()) {
                        Drawable img = Drawable.createFromPath(f.getAbsolutePath());

                        l.setBackgroundDrawable(img); <-- Why is this not working?||||||

                        store = PreferenceManager.getDefaultSharedPreferences(this);
                        edit = store.edit();
                        edit.putString("my_image", f.getAbsolutePath());
                        edit.commit();
                    }

getRealPathFromUri

private String getRealPathFromUri(Uri contentURI) // For Changing Wallpaper
{
    Cursor cursor = getContentResolver().query(contentURI, null, null,
            null, null);
    if (cursor == null) {
        return contentURI.getPath();
    } else {
        cursor.moveToFirst();
        int idx = cursor
                .getColumnIndex(MediaStore.Images.ImageColumns.DATA);
        return cursor.getString(idx);

    }
}
}

1 个答案:

答案 0 :(得分:0)

终于自己解决了。

将上述wallp()方法更改为:

private void wallp() // For changing wallpaper
{
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.putExtra("crop", "true");
    intent.putExtra("aspectX", 1);
    intent.putExtra("aspectY", 1);
    intent.putExtra("outputY", 630);
    intent.putExtra("outputX", 720);
    intent.putExtra("return-data", true);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"),
            SELECT_PICTURE);

}