0

我试图显示一个可绘制的图像,通过它从 web 服务动态加载图像(获取图像 URL)。

这是我的代码,

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.property_image_adapter);
    Intent intent=getIntent();

    Gallery propertyImageGallery=(Gallery)findViewById(R.id.gallery1);
    intent.getStringExtra("PropertyID");

    PropertyImagesList ImageList=new PropertyImagesList();
    img=ImageList.fetchPropertyImages(intent.getStringExtra("PropertyID"));

    for(int i=0;i<=img.size()-1;i++){

        try {
            Images[i]=drawableFromUrl(img.get(i).Source);//Here I am trying to save the url to the Images variable(eg: www.gggg.com/aa.jpg"). Don't know if this is correct.
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }       
    ImageAdapter=new PropertyImageAdapter(this,Images);
    propertyImageGallery.setAdapter(ImageAdapter);      
}

@SuppressWarnings("deprecation")
public static Drawable drawableFromUrl(String url) throws IOException {
    Bitmap x;

    HttpURLConnection connection = (HttpURLConnection) new URL("SOME URL APPENDING"+url.trim()).openConnection();
    connection.connect();
    InputStream input = connection.getInputStream();
    x = BitmapFactory.decodeStream(input);
    return new BitmapDrawable(x);
}
}

这是我的 ImageAdapter 类,

public class PropertyImageAdapter extends BaseAdapter {
private Context ctx;
Drawable[] pics;

public PropertyImageAdapter(Context c,Drawable[] pics) {
    ctx = c; 
    this.pics=pics;
}

@Override
public int getCount() {

    return pics.length;
}

@Override
public Object getItem(int arg0) {

    return arg0;
}

@Override
public long getItemId(int arg0) {

    return arg0;
}

@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {

    ImageView iView = new ImageView(ctx);
    iView.setImageDrawable(pics[arg0]);
    iView.setScaleType(ImageView.ScaleType.FIT_XY);
    iView.setLayoutParams(new Gallery.LayoutParams(150, 150));
    return iView;
}

}

这是布局文件,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
>

 <Gallery
    android:id="@+id/gallery1"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:spacing="10dip"
    >
 </Gallery>
</LinearLayout>

我将图像 URL 添加到图像 [] 变量中,并通过 ImageAdapter 类将变量中的所有图像设置为布局文件中的库项。

在此行之后“Images[i]=drawableFromUrl(img.get(i).Source);” 我收到 NullPointerException

这是我的日志猫,

06-19 13:34:29.921: E/AndroidRuntime(16365): FATAL EXCEPTION: main
06-19 13:34:29.921: E/AndroidRuntime(16365): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bu/com.bu.PropertySearchTypes.PropertyImageGallery}: java.lang.NullPointerException
06-19 13:34:29.921: E/AndroidRuntime(16365):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
06-19 13:34:29.921: E/AndroidRuntime(16365):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
06-19 13:34:29.921: E/AndroidRuntime(16365):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-19 13:34:29.921: E/AndroidRuntime(16365):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
06-19 13:34:29.921: E/AndroidRuntime(16365):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-19 13:34:29.921: E/AndroidRuntime(16365):    at android.os.Looper.loop(Looper.java:130)
06-19 13:34:29.921: E/AndroidRuntime(16365):    at android.app.ActivityThread.main(ActivityThread.java:3687)
06-19 13:34:29.921: E/AndroidRuntime(16365):    at java.lang.reflect.Method.invokeNative(Native Method)
06-19 13:34:29.921: E/AndroidRuntime(16365):    at java.lang.reflect.Method.invoke(Method.java:507)
06-19 13:34:29.921: E/AndroidRuntime(16365):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
06-19 13:34:29.921: E/AndroidRuntime(16365):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
06-19 13:34:29.921: E/AndroidRuntime(16365):    at dalvik.system.NativeStart.main(Native Method)
06-19 13:34:29.921: E/AndroidRuntime(16365): Caused by: java.lang.NullPointerException
06-19 13:34:29.921: E/AndroidRuntime(16365):    at com.bu.PropertySearchTypes.PropertyImageGallery.onCreate(PropertyImageGallery.java:47)
06-19 13:34:29.921: E/AndroidRuntime(16365):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-19 13:34:29.921: E/AndroidRuntime(16365):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)

请帮助我找出异常的原因。

这是从 Web url 将图像加载到图库的正确方法吗?如果在我的代码中发现任何错误的实现,请更正。

任何帮助表示赞赏!谢谢!!

4

1 回答 1

0

您应该不从 UI 线程调用 drawableFromUrl() 方法。发现此文档:http: //developer.android.com/guide/components/processes-and-threads.html

我建议您使用 UniversalImageLoader 从 Internet 加载图像,查看该库,您不需要实现自己的下载器。https://github.com/nostra13/Android-Universal-Image-Loader#license

于 2013-06-19T08:37:01.763 回答