0

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.

RhysBailey21
  • 117
  • 3
  • 13

2 Answers2

0

Little change your code i hope it's help for you

Updated:

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();
    Bitmap bitmap = BitmapFactory.decodeFile(picturePath);
    BitmapDrawable background = new BitmapDrawable(getApplicationContext().getResources(),bitmap);
    llmain.setBackground(background);
Joy
  • 289
  • 1
  • 15
  • this doesn't work due to the target for the bitmap being a LinearLayout and not an ImageView – RhysBailey21 Mar 22 '17 at 05:24
  • no this still doesn't work. It just gives me a blank white background. Im now not sure if its something to do with the retrieval of the image or the setting of background part. – RhysBailey21 Mar 22 '17 at 08:49
0

Set Bitmap as background to linear layout.

Directly you can't set bitmap to layout so you should use BitmapDrawable which convert bitmap to drawable.

BitmapDrawable background = new BitmapDrawable(bmImg);
linearLayout.setBackgroundDrawable(background);
ViramP
  • 1,659
  • 11
  • 11
  • This method is depreciated. Since API 22 you can no longer do this on newer devices. It works for API 22 or less – RhysBailey21 Mar 22 '17 at 08:45
  • yes, You can use linearLayout.setBackground(background); FYI: public void setBackground(Drawable background) { throw new RuntimeException("Stub!"); } – ViramP Mar 22 '17 at 08:50