我有一个应用程序,可以从用户图库中获取选定的图像并将其显示为应用程序中的背景。我已经成功使用了下面的代码。
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() 调用是针对 context 和 int 而不是 context 和 BitmapDrawable。