-1

我无法从 URL 加载图像,指令 InputStream 是 = (InputStream) new 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>

怎么了?

4

2 回答 2

1

使用毕加索:

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

它还自动处理缓存。

于 2016-01-29T15:55:24.947 回答
0

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

于 2013-09-15T09:56:16.887 回答