来自URL的android加载图片不起作用

时间:2013-09-15 09:52:36

标签: android url imageview

我无法从URL加载图像,指令InputStream是=(InputStream)新的URL(url).getContent();抓住一个例外 URl有效,也许我忘了什么?

我的活动:

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



    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View rowView = inflater.inflate(rowResourceId, parent, false);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.imageView);
    TextView textView = (TextView) rowView.findViewById(R.id.textView);

    int id = Integer.parseInt(Ids[position]);
    String imageFile = Model.GetbyId(id).IconFile;
    Drawable drawable = LoadImageFromWebOperations("http://www.androidpeople.com/wp-content/uploads/2010/03/android.png");

    imageView.setImageDrawable(drawable);

    textView.setText(Model.GetbyId(id).Name);
return rowView;

}

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

}

这是我的明确许可:

<uses-sdk
    android:minSdkVersion="10"
    android:targetSdkVersion="10" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="yourello.maga.SplashActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
     <activity
    android:name="yourello.maga.MainActivity"
    android:label="@string/app_name" />
     <activity
    android:name="yourello.maga.listGallery"
    android:label="@string/app_name" />         
</application>

出了什么问题?

2 个答案:

答案 0 :(得分:1)

使用毕加索:

Picasso.with(context.this).load("http://www.androidpeople.com/wp-content/uploads/2010/03/android.png").into(imageView);

它还会自动处理缓存。

答案 1 :(得分:0)

使用通用图像加载器下载图像,它将自动处理所有内容,这里是链接https://github.com/nostra13/Android-Universal-Image-Loader,它也会处理图像缓存。