2012-04-09 10 views
0

92行目のURLから画像を取得する際に問題が発生しました。解決してください。Android RSS画像の問題

import java.io.IOException; 
import java.io.InputStream; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.util.ArrayList; 

import android.app.Activity; 
import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.drawable.Drawable; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.ImageView; 
import android.widget.TextView; 

public class EfficientAdapter extends BaseAdapter { 
    private Activity activity; 
    private ArrayList<Post> data; 
    private ImageView iv; 
    private static LayoutInflater inflater = null; 
    // public ImageLoader imageLoader; 
    ViewHolder holder; 

    EfficientAdapter(Activity a, ArrayList<Post> d) { 

     activity = a; 
     data = d; 
     inflater = (LayoutInflater) activity 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     // imageLoader = new ImageLoader(activity.getApplicationContext()); 

    } 

    @Override 
    public int getCount() { 
     return data.toArray().length; 

    } 

    @Override 
    public Object getItem(int position) { 

     return position; 
    } 

    @Override 
    public long getItemId(int position) { 

     return position; 
    } 

    public static class ViewHolder { 
     public TextView label; 
     public TextView addr; 
     public ImageView image; 
    } 

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

     if (convertView == null) { 
      vi = inflater.inflate(R.layout.row, null); 
      holder = new ViewHolder(); 
      holder.label = (TextView) vi.findViewById(R.id.title); 
      holder.addr = (TextView) vi.findViewById(R.id.details); 
      holder.image = (ImageView) vi.findViewById(R.id.thumb); 
      vi.setTag(holder); 
     } else 
      holder = (ViewHolder) vi.getTag(); 

     holder.label.setText(data.get(position).getTitle()); 
     holder.addr.setText(data.get(position).getPubDate()); 

     // imageLoader.DisplayImage((data.get(position).getThumbnail()), 
     // activity, 
     // holder.image, 72, 72); 
     URL url = null; 
     try { 
      url = new URL((data.get(position).getThumbnail())); 
     } catch (MalformedURLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

//ここではエラーに直面しています。あなたはlog catのエラーを見ることができます。

InputStream content = null; 
      try { 
       content = (InputStream) url.getContent(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      Drawable d = Drawable.createFromStream(content, "src"); 
      Bitmap mIcon1 = null; 
      try { 
       mIcon1 = BitmapFactory.decodeStream(url.openConnection() 
         .getInputStream()); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      holder.image.setImageBitmap(Bitmap.createScaledBitmap(mIcon1, 72, 72, 
        false)); 
        return vi; 
     } 

    }   

これは私の例ですが、このドロップボックスにZIPファイルを入れました。 リンク:https://dl-web.dropbox.com。 お願いします。 92行のEfficientAdapter.javaに問題があります。nullポインタ例外でエラーが発生しました。

答えて

0

使用URLから画像を取得するため、この機能..私はGetViewメソッドに画像を設定することができますどのように

Drawable img = ImageOperations(ctx, url); 
holder.image.setImageDrawable(img); 

public Object fetch(String address) throws MalformedURLException, IOException { 

    URL url = new URL(address); 
    Object content = url.getContent(); 
    return content; 
} 

private Drawable ImageOperations(Context ctx, String url) { 

    try { 
     InputStream is = (InputStream) this.fetch(url); 
     Drawable d = Drawable.createFromStream(is, "src"); 
     return d; 
    } catch (MalformedURLException e) { 
     return null; 
    } catch (IOException e) { 
     return null; 
    } 
} 
+0

先生。 –

+0

今すぐチェック.... –

+0

私はイメージを取得できませんでした。 –