Android - 使用hashmap从uri解析图像会给我带来错误

时间:2012-12-22 09:05:27

标签: java android json image parsing

我需要在xml中解析json输出。解析已完成但图像未在xml中看到。 Android日志说明了这个 -

12-22 14:26:34.472: I/System.out(6037): resolveUri failed on bad bitmap uri: "base_url"/sites/default/files/pictures/picture-6175010166.jpg

我的下面的代码有什么问题,任何机构都可以建议我吗?

HashMap<String, String> listview = new HashMap<String, String>();
String title = "";
String teaser="";
String createdon = "";
String profile_image = "";      
try {
  title = jListview.getString("title");
  teaser = jListview.getString("teaser");
  createdon = jListview.getString("created");
  profile_image = jListview.getString("profile_image");
  listview.put("title", title);
  listview.put("teaser", teaser);
  listview.put("created", createdon);
  listview.put("profile_image", profile_image);
  //listview.put("profile_image", picture);
} catch (JSONException e) { 
  System.out.println( "Bad Error" + e.toString());
  e.printStackTrace();
} 
return listview;

这是我的主要活动代码,其中iam显示结果

         try{
        /** Getting the parsed data as a List construct */
            lists = listJsonParser.parse(obj);
            int imageCount = lists.size();
            }

        }catch(Exception e){
            Log.d("Exception Main",e.toString());
        }          

        /** Keys used in Hashmap */
        String[] from = { "title","teaser","created","profile_image"};

        /** Ids of views in listview_layout */
        int[] to = { R.id.title,R.id.teaser,R.id.createdon,R.id.list_image};

        /** Instantiating an adapter to store each items
        *  R.layout.listview_layout defines the layout of each item
        */
        SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), countries, R.layout.home_layout, from, to);  

        return adapter;

            LoGcat:12-22 14:26:34.382: I/System.out(6037): resolveUri failed on bad bitmap uri: 

2 个答案:

答案 0 :(得分:0)

您无法将直接网址设置为ImageView来设置ImageView src。首先通过为ListView扩展BaseAdapter来创建自定义适配器,在getView中,您需要先将Url中的图像作为位图下载,然后将其设置为ImageView:

public class CustomAdapter extends BaseAdapter{

        @Override
        public View getView(int position, View convertView,
                                 ViewGroup parent) {

        if(convertView == null){
        LayoutInflater layoutInflater = (LayoutInflater) 
                                getSystemService(LAYOUT_INFLATER_SERVICE);
        convertView = layoutInflater.
                                     inflate(R.layout.layout_row, null);
         }

        Bitmap test = getbitpam(maptemp.get("profile_image"));
        imgview=(ImageView) convertView.findViewById(R.id.img_list);
        imgview.setImageBitmap(test);
        return convertView;
    }
}
//get image from server 
public  Bitmap getbitpam(String url) {
    Bitmap bitmap = null;
    InputStream in = null;
    BufferedOutputStream out = null;

    try {
        in = new BufferedInputStream(new URL(url).openStream(), IO_BUFFER_SIZE);

        final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
        out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
        copy(in, out);
        out.flush();

        final byte[] data = dataStream.toByteArray();
        BitmapFactory.Options options = new BitmapFactory.Options();
        //options.inSampleSize = 1;

        bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,options);
    } catch (IOException e) {
        Log.e(TAG, "Could not load Bitmap from: " + url);
    } finally {
        closeStream(in);
        closeStream(out);
    }

    return bitmap;
}


}

答案 1 :(得分:0)

你的错误说下面:

12-22 14:26:34.472: I/System.out(6037): resolveUri failed on bad bitmap uri: "base_url"/sites/default/files/pictures/picture-6175010166.jpg

您需要下载图像,然后将其设置为位图。 HERE是众多例子中的一个。