私は、ユーザーギャラリーから選択した画像を受け取り、アプリ内でその背景として表示できるアプリを持っています。私は成功のために以下のコードを使用しました。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);
これは私の更新ANSをチェック@RhysBaileyビットマップがのLinearLayoutはなくImageViewの – RhysBailey21
であることのために起因ターゲットに動作しません。.. – Joy
いいえ、これはまだ動作しません。それはちょうど空白の白い背景を私に与える。画像の検索や背景部分の設定と関係があるかどうかは不明です。 – RhysBailey21