我正在从图库中将背景图像设置为线性布局,除非图像尺寸太大,否则它工作正常,我已经实现了异常处理,如果有任何异常,那么我正在从资源设置默认背景,但仍然是我的应用程序正在崩溃。这是我的代码...
public static void setBackgroundImage(Context context,
LinearLayout linearLayout, Uri uri) {
try {
Bitmap bitmap = BitmapFactory.decodeStream(context
.getContentResolver().openInputStream(uri));
Drawable drawable = new BitmapDrawable(context.getResources(),
bitmap);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
setImageForJellyBeanOrLater(linearLayout, drawable);
} else {
linearLayout.setBackgroundDrawable(drawable);
}
} catch (Exception e) {
linearLayout.setBackgroundResource(R.drawable.ic_bg_image);
e.printStackTrace();
}
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private static void setImageForJellyBeanOrLater(LinearLayout linearLayout,
Drawable drawable) {
linearLayout.setBackground(drawable);
}