PRP PRP - 5 months ago 12
Java Question

Image view not showing

I have a code which adds buttons and images dynamically to a fragment. The buttons are getting added but the images are not being added.

Here is my code :

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 and params2 are defined as follows :

LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

LayoutParams params2 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);


Here is the output on the emulator :

Here is my assets folder :

Answer

Better to use iterative strategy..

first, new ImageView(getContext()); -> new ImageView(**getActivity()**);

Secondly, if you don't know where is the problem, then better reading default drawable image

img_view.setImageResource(R.drawable.<any_dummy_image_from_drawable>);

instead of

setImageBitmap()

if your dummy image load properly that means your View is perfect and having problem while reading from assets, If so, then you can concentrate more on asset reading thing.