从位图获取URI?

时间:2011-11-29 14:30:11

标签: android image listview error-handling uri

我尝试做什么


我有一个ListView,我已经填充了我的SimpleListAdapter,数据来自JSON-File。现在我写了一个ImageDownloader,我想将Image转换为ListView。但我总是在这里得到这个错误:resolve Uri failed on bad bitmap uri.

问题


我需要更改以使代码正常工作。请提前帮助你! 你可以在这里找到代码!

代码


ChannelActivity.class

package de.stepforward;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


import org.json.JSONArray;
import org.json.JSONObject;

import de.stepforward.web.ShowVideo;


import android.app.ListActivity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;



public class ChannelActivity extends ListActivity {


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


    String result = "";
    String line = null;
    final ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();


    //get the Data from URL
    try{
    URL url = new URL("http://gdata.youtube.com/feeds/mobile/users/TheStepForward/uploads?alt=json&format=1"); 

    URLConnection conn = url.openConnection();
    StringBuilder sb = new StringBuilder();
    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));

    //read d response till d end
    while ((line = rd.readLine()) != null) {
    sb.append(line + "\n");
    }
    result = sb.toString();
    Log.v("log_tag", "Append String " + result);
    } catch (Exception e) {
    Log.e("log_tag", "Error converting result " + e.toString());
    }

    try{
        JSONObject json = new JSONObject(result);
        JSONObject feed = json.getJSONObject("feed");
        JSONArray entrylist = feed.getJSONArray("entry");

        for(int i=0;i<entrylist.length();i++){
            //Get Title
            JSONObject movie = entrylist.getJSONObject(i);
            JSONObject title = movie.getJSONObject("title");
            String txtTitle = title.getString("$t");
            Log.d("Title", txtTitle);

            //Get Description
            JSONObject content = movie.getJSONObject("content");
            String txtContent = content.getString("$t");
            Log.d("Content", txtContent);

            //Get Link
            JSONArray linklist = movie.getJSONArray("link");
            JSONObject link = linklist.getJSONObject(0);
            String txtLink = link.getString("href");
            Log.d("Link", txtLink);


            //Get Thumbnail
            JSONObject medialist = movie.getJSONObject("media$group");
            JSONArray thumblist = medialist.getJSONArray("media$thumbnail");
            JSONObject thumb = thumblist.getJSONObject(2);
            String txtThumb = thumb.getString("url");
            Log.d("Thumb", txtThumb.toString());

            //ImageLoader
            Bitmap bitmap = getImageBitmap(txtThumb);

            String picture = bitmap.toString();

            //String Array daraus machen und in Hashmap füllen
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("Thumb", picture);
            map.put("Title", txtTitle);
            map.put("Content", txtContent);
            map.put("Link", txtLink);
            mylist.add(map);

        }
        //ListView füllen
        ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.lit, 
                new String[] { "Thumb","Title","Content","Link"}, 
                new int[] { R.id.img_video,R.id.txt_title,R.id.txt_subtitle});      
        setListAdapter(adapter);


        //OnClickLister um Youtube-Video zu öffnen
        final ListView lv = getListView();
            lv.setOnItemClickListener(new OnItemClickListener(){
                public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

                    //Video-Link auslesen
                    Map<String, String> map = mylist.get(position);
                    String link = map.get("Link");
                    Log.d("Link", link);

                    //Link übergeben, Activity starter
                    final Intent Showvideo = new Intent(ChannelActivity.this, ShowVideo.class);
                    Showvideo.putExtra("VideoLink", link);
                    startActivity(Showvideo);


                }
            });



    }catch (Exception e) {
        Log.e("log_tag", "Error converting result " + e.toString());
        }
    }
    private Bitmap getImageBitmap(String url) {
        Bitmap bm = null;
        try {
            URL aURL = new URL(url);
            URLConnection conn = aURL.openConnection();
            conn.connect();
            InputStream is = conn.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            bm = BitmapFactory.decodeStream(bis);
            bis.close();
            is.close();
        } catch (IOException e) {
            Log.e("SHIT", "Error getting bitmap", e);
        }
        return bm;
    } 

}

2 个答案:

答案 0 :(得分:1)

您最有可能尝试将.jpg或.png解码为位图。

看看这个问题&amp;答案。

resolveUri failed on bad bitmap uri” when putting image on ListView

答案 1 :(得分:0)

我认为问题是SimpleAdapter仅适用于TextViews,看起来您正在尝试使用它来处理ImageView。

您需要创建ListAdapter并在getView方法中手动设置视图中的数据。