【问题标题】:"resolveUri failed on bad bitmap uri" when putting image on ListView将图像放在 ListView 上时,“resolveUri 在错误的位图 uri 上失败”
【发布时间】:2011-02-23 18:57:19
【问题描述】:

嘿, 我是 android 新手,我有一个小问题。我正在尝试将一个列表视图与通过 URL 加载的图像组合在一起。到目前为止,这有效,但图像不会显示。 logcat 是这样说的:

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

它显然没有找到我的图像,但我不知道为什么。使用 R.drawable.icon 可以。谁能帮我吗?这是我的来源:

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;
        }
    }
}

提前致谢。 抢

【问题讨论】:

    标签: android json listview drawable


    【解决方案1】:

    我已经解决了这个问题。 我正在使用代码来解决问题。

    这里的“路径”是一个本地文件系统路径,可以使用: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;
    
        }
    }
    

    希望这会有所帮助:)。

    【讨论】:

    • 请描述如何通过json在imageview上添加图片
    猜你喜欢
    • 2012-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-30
    • 2014-07-16
    • 2020-12-23
    相关资源
    最近更新 更多