Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
716 views
in Technique[技术] by (71.8m points)

android - "resolveUri failed on bad bitmap uri" when putting image on ListView

hey, i'm new to android and i have a slight problem. i'm trying to put together a listview with images are loaded through a URL. this works so far, but the images wont show up. logcat says this:

02-23 19:20:48.447: INFO/System.out(833): resolveUri failed on bad bitmap uri: android.graphics.drawable.BitmapDrawable@405359b0

its obviously not finding my image, but i have no idea why. using R.drawable.icon works though. can anyone help me out? heres my source:

public class Main extends Activity {
        private ListView lv_main;

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

        lv_main = (ListView) findViewById(R.id.listviewmain);

        ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();

        HashMap<String,String> map;

        map = new HashMap<String, String>();
        map.put("title", "a title goes here");
        map.put("details", "details blaa");
        map.put("cover", String.valueOf(Main.this.LoadImageFromWebOperations("http://www.google.com/intl/en_ALL/images/srpr/logo1w.png"));
        //THIS WORKS FINE: map.put("cover", String.valueOf(R.drawable.icon));
        listItem.add(map);

        map = new HashMap<String, String>();
        map.put("title", "2nd title");
        map.put("details", "more details");
        map.put("cover", String.valueOf(Main.this.LoadImageFromWebOperations("http://www.google.com/images/srpr/nav_logo37.png"));
        //THIS WORKS FINE: map.put("cover", String.valueOf(R.drawable.icon));
        listItem.add(map);

        SimpleAdapter mSchedule = new SimpleAdapter (this.getBaseContext(), listItem, R.layout.listviewitem, new String[] {"cover", "title", "details"}, new int[] {R.id.cover, R.id.title, R.id.details});

        lv_main.setAdapter(mSchedule);
     }

    public Drawable LoadImageFromWebOperations(String url) {
        try
        {
                InputStream is = (InputStream) new URL(url).getContent();
                Drawable d = Drawable.createFromStream(is, "src name");
                return d;
        }catch (Exception e) {
                System.out.println("Exc="+e);
                return null;
        }
    }
}

thanks in advance. rob

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I have resolved this issue. I am using the code to solve the issue.

Here 'Path' is a local file System path that can be got using: Environment.getExternalStorageDirectory().getAbsolutePath()

public static String loadImageFromWebOperations(String url, String path) {
    try {
        InputStream is = (InputStream) new URL(url).getContent();

        System.out.println(path);
        File f = new File(path);

        f.createNewFile();
        FileOutputStream fos = new FileOutputStream(f);
        try {

            byte[] b = new byte[100];
            int l = 0;
            while ((l = is.read(b)) != -1)
                fos.write(b, 0, l);

        } catch (Exception e) {

        }

        return f.getAbsolutePath();
    } catch (Exception e) {
        System.out.println("Exc=" + e);
        return null;

    }
}

Hope this helps :).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...