2017-03-22 8 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のためであるので、それは動作しません。リニアレイアウトの背景として

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

リトルは、私はそれが更新さをあなた

ための助け願っていますあなたのコードを変更します。

直接ビットマップをレイアウトに設定できないため、ビットマップを描画可能に変換するBitmapDrawableを使用する必要があります。

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

これは私の更新ANSをチェック@RhysBaileyビットマップがのLinearLayoutはなくImageViewの – RhysBailey21

+0

であることのために起因ターゲットに動作しません。.. – Joy

+0

いいえ、これはまだ動作しません。それはちょうど空白の白い背景を私に与える。画像の検索や背景部分の設定と関係があるかどうかは不明です。 – RhysBailey21

0

セットのビットマップ:

+0

このメソッドは償却されます。 API 22以降は、新しいデバイスでこれを行うことはできません。 API 22以下で動作します – RhysBailey21

+0

はい、 linearLayout.setBackground(背景)を使用できます。 FYI: パブリックvoid setBackground(描画可能な背景){ throw new RuntimeException( "Stub!"); } – ViramP