Android : Cropping an image and setting it as background

on Thursday, August 14, 2014


Whenever I select a picture from gallery to set it as a background, it shrinks (if size exceeds the device resolution). So I cropped the selected image before setting it as a background. But somehow its not setting it as background after cropping. Must be a silly mistake I know but I'm not able to figure it out right now.


Thanks.


Can someone point out what's the problem?



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);

}
}
}

0 comments:

Post a Comment