Java 图像视图未显示
我有一个将按钮和图像动态添加到片段的代码。正在添加按钮,但未添加图像 这是我的密码:Java 图像视图未显示,java,android,android-imageview,Java,Android,Android Imageview,我有一个将按钮和图像动态添加到片段的代码。正在添加按钮,但未添加图像 这是我的密码: for(String f1 : f){ // For Each File Add a View or a button or something LogUtil.d(TAG,"ocr override" + f1); Button b = new Button (getActivity()); b.setId (i); b.setLayoutParams(params
for(String f1 : f){
// For Each File Add a View or a button or something
LogUtil.d(TAG,"ocr override" + f1);
Button b = new Button (getActivity());
b.setId (i);
b.setLayoutParams(params2);
b.setText (f1);
b.setOnClickListener(this);
ImageView img_view = new ImageView(getContext());
img_view.setImageBitmap(getBitmapFromAsset("OcrSampleImages"+System.getProperty("file.separator")+f1));
img_view.setLayoutParams(params);
linearLayout.addView(b);
linearLayout.addView(img_view,params);
i++;
}
params和params2的定义如下:
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
LayoutParams params2 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
以下是模拟器上的输出:
这是我的资产文件夹:最好使用迭代策略 首先,
newimageview(getContext())代码>->新建图像视图(**getActivity()**)代码>
其次,如果您不知道问题出在哪里,那么最好阅读默认的可绘制图像
img_view.setImageResource(R.drawable.<any_dummy_image_from_drawable>);
如果虚拟图像加载正确,则意味着您的视图是完美的,在从资产读取时出现问题,
如果是这样的话,那么你可以更专注于资产读取的事情你可以发布你的getBitmapFromAsset(String)
方法吗?我尝试从R.drawable添加一个图像,结果成功了……那么setImageBitmap()可能有什么问题?“OcrSampleImages”+System.getProperty(“file.separator”)+f1可能不存在,对于资产目录结构,assets/image/abc.jpg示例代码为AssetManager AssetManager=context.getAssets();输入流istr;位图=空;尝试{istr=assetManager.open(“image/abc.jpg”);位图=BitmapFactory.decodeStream(istr);}捕获(IOException e){e.printStackTrace();}返回位图;该行有什么问题?最好在日志中打印“OcrSampleImages”+System.getProperty(“file.separator”)+f1,以检查其是否正确。。这将帮助您自己调试。。作为一种实践…(不知道是否需要),尝试在资产的文件夹和文件名中使用小写字母。
setImageBitmap()