0

Possible Duplicate:
Android: Display Image from SD CARD

Here's my current code:

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.LayoutParams;

public class DisplayImageMapActivity extends Activity {
    ImageView iv;
    private final String FLOOR = "F";
    private final String storagePath = Environment.getExternalStorageDirectory() + "/AppH23";
    private final String localMapsPath = storagePath + "/localMaps";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        iv=(ImageView)findViewById(R.id.storageimage);

        RadioGroup levelRGroup = (RadioGroup) findViewById(R.id.level_rgroup);

        int levelSize = 8;
        for (int i = 0; i < levelSize; i++) {
            RadioButton levelRButton = new RadioButton(this);
            if(i==0) {
                levelRButton.setText(new StringBuffer(i+1).append(FLOOR).append("(start)"));
            } else if (i==7) {
                levelRButton.setText(new StringBuffer(i+1).append(FLOOR).append("(end)"));
            }
            levelRButton.setTag((i+1) + FLOOR);
            levelRButton.setLayoutParams(
                    new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1));

            levelRGroup.addView(levelRButton);
        }

        levelRGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            public void onCheckedChanged(RadioGroup group, final int checkedId) {
                iv.setImageURI(Uri.parse(new StringBuffer(localMapsPath)
                .append("/").append(group.findViewById(checkedId).getTag()).append(".gif").toString()));
                iv.invalidate();
            }
        });

        levelRGroup.getChildAt(0).performClick();

    }
} 

Here's my environment: a) 2.1-update1

It kinda strange, my image from /sdcard/AppH23/localMaps/???.gif doesn't show. all gif files are confirmed that are existing.

UPDATE: here's my previous post and you may confirm that the gif image was displayed a while ago. gif image displayed

Community
  • 1
  • 1
eros
  • 4,946
  • 18
  • 53
  • 78
  • ` new RadioButton(this);` creating objects like this will lead to memory leak! [see](http://android-developers.blogspot.com/2009/01/avoiding-memory-leaks.html) – Vikas Sep 06 '11 at 07:31
  • i tried that code a while ago, then I successfully displayed the image. But now, kinda weird. @CapDroid No any error. – eros Sep 06 '11 at 07:35
  • @Vikas memory leak? what should I do? – eros Sep 06 '11 at 07:35
  • oohhh even I removed the RadioGroup and RadioButtons. Just leaving the ImageView. :( – eros Sep 06 '11 at 07:44

1 Answers1

0

Try to make levelRGroup a class variable(similar to iv). It is currently a local variable and it will be garbage collected at some point after onCreate returns. When it will be deleted cannot be known and that would cause inconsistent behaviour even if you do not change anything in your code.

Caner
  • 57,267
  • 35
  • 174
  • 180