Note that there are some explanatory texts on larger screens.

plurals
  1. POImageView unable to display images one by one
    primarykey
    data
    text
    <p><strong>What I'm trying to do:</strong> 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.</p> <p><strong>What problem I'm having:</strong> 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.</p> <p>Here is my <strong>layout.xml</strong> file (I'd like to handle images of different sizes, but will worry about it later):</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;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" &gt; &lt;ImageView android:id="@+id/image_display" android:layout_width="400dp" android:layout_height="400dp" &gt; &lt;/ImageView&gt; &lt;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" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>Here is my <strong>code</strong>:</p> <pre><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 &lt; 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); } } } </code></pre> <p>I found some other discussions about similar problems <a href="https://stackoverflow.com/questions/4458565/imageview-not-refreshing-reflecting-changes">here</a> and <a href="https://stackoverflow.com/questions/2859212/how-to-clear-an-imageview-in-android">here</a>. I tried a few suggested solutions, e.g. adding setImageDrawable(null) to force ImageView to update; that did not work for me.</p> <p>Anything wrong with my approach? I really appreciate your helps and suggestions.</p> <p>Thanks in advance &amp; Have a nice weekend.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload