I have an app that can take a selected image from the users gallery and display it as their background within the app. I have used the code below with success.
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();
BitmapDrawable drawable = new BitmapDrawable(getResources(), BitmapFactory.decodeFile(picturePath));
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
llmain.setBackgroundDrawable(drawable);
} else if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP_MR1) {
llmain.setBackground(drawable);
}
Although, since I have updated my app to API 22 it has stopped working. I have found how to set the background with
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
llmain.setBackgroundDrawable(drawable);
} else if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP_MR1) {
llmain.setBackground(drawable);
} else {
llmain.setBackground(ContextCompat.getDrawable(this, drawable));
}
But it doesn't work since the ContextCompat.getDrawable() call is for context and an int and not context and a BitmapDrawable.