2017-03-22 7 views
0

사용자 갤러리에서 선택한 이미지를 가져 와서 앱 내 배경으로 표시 할 수있는 앱이 있습니다. 나는 아래 코드를 성공과 함께 사용했다.setBackground from Bitmap API 22+

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

내 앱이 API 22로 업데이트 되었기 때문에 작동이 중지되었습니다. 나는

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

와 배경을 설정하지만 ContextCompat.getDrawable() 호출 컨텍스트와의 int가 아닌 컨텍스트와 BitmapDrawable에 대한 때문에 작동하지 않는 방법을 발견했다.

답변

0

거의 변화 코드를 내가 당신

업데이트에 대한 도움말 희망 : 선형 레이아웃 배경으로

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

이 때문에있는 LinearLayout과 이미지 뷰 – RhysBailey21

+0

내 업데이트 ANS를 확인 @RhysBailey하지 ... – Joy

+0

더이 여전히 작동하지 않습니다되는 비트 맵의 ​​대상에 작동하지 않습니다. 그냥 빈 흰색 배경을 제공합니다. 이미지의 검색 또는 배경 부분의 설정과 관련이 있는지는 확실하지 않습니다. – RhysBailey21

0

설정 비트 맵.

직접 비트 맵을 레이아웃으로 설정할 수 없으므로 비트 맵을 드로어 블로 변환하는 BitmapDrawable을 사용해야합니다.

BitmapDrawable background = new BitmapDrawable(bmImg); 
linearLayout.setBackgroundDrawable(background); 
+0

이 방법은 가치가 떨어집니다. API 22 이후 더 이상 새로운 기기에서이 작업을 수행 할 수 없습니다. API 22 이하에서 작동합니다. – RhysBailey21

+0

예, linearLayout.setBackground (background); FYI : public void setBackground (Drawable background) { throw new RuntimeException ("스텁!"); } – ViramP