What I'm trying to do: I have several JPG files saved in a subfolder under the "assets" folder. My app should go into this subfolder, load the first file, display it for 30 seconds, then load the second file, display it for 30 seconds, and so on, ..., until the last file. This is done in a loop.
What problem I'm having: The app goes through all the iterations of the loop. It loads each file correctly, reports the width and height of each image correctly. However, the screen is black with no images being displayed, until after the whole loop finishes. Then the last image is displayed.
Here is my layout.xml file (I'd like to handle images of different sizes, but will worry about it later):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mylayout_id"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/image_display"
android:layout_width="400dp"
android:layout_height="400dp" >
</ImageView>
<Button
android:id="@+id/button_start"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="pressedStart"
android:text="@string/btn_start" />
</LinearLayout>
Here is my code:
public class MyImageActivity extends Activity {
private final String TAG = "MyImageActivity";
private ImageView mIV;
private Bitmap mFaceBitmap;
private AssetManager mAssetMan = null;
private String[] mImgFileNames = null;
private int mBitmapWidth;
private int mBitmapHeight;
@Override
public void onCreate(Bundle savedInstanceState) {
Log.e(TAG, "onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout);
mIV = (ImageView) findViewById(R.id.image_display);
}
@Override
protected void onResume() {
Log.e(TAG, "onResume");
super.onResume();
mAssetMan = getAssets();
}
public void pressedStart(View view) {
Log.e(TAG, "pressedStart");
try {
mImgFileNames = mAssetMan.list("my_subfolder");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Log.d(TAG, "mImgFileNames = " + mImgFileNames);
Log.d(TAG, "number of image files = " + mImgFileNames.length);
for (int i = 0; i < mImgFileNames.length; i++) {
Log.d(TAG, "image file name = " + mImgFileNames[i]);
mIV.setImageDrawable(null);
mIV.invalidate();
InputStream istr = null;
try {
istr = mAssetMan.open("my_subfolder" + mImgFileNames[i]);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// load the photo
Bitmap b = BitmapFactory.decodeStream(istr);
mFaceBitmap = b.copy(Bitmap.Config.RGB_565, true);
b.recycle();
mBitmapWidth = mFaceBitmap.getWidth();
mBitmapHeight = mFaceBitmap.getHeight();
Log.d(TAG, "mBitmapWidth = " + mBitmapWidth);
Log.d(TAG, "mBitmapHeight = " + mBitmapHeight);
Drawable drawable = new BitmapDrawable(getResources(), mFaceBitmap);
mIV.setImageDrawable(drawable);
mIV.invalidate();
SystemClock.sleep(5000);
}
}
}
I found some other discussions about similar problems here and here. I tried a few suggested solutions, e.g. adding setImageDrawable(null) to force ImageView to update; that did not work for me.
Anything wrong with my approach? I really appreciate your helps and suggestions.
Thanks in advance & Have a nice weekend.