在Android中通过Web服务显示来自数据库的图像

时间:2010-12-30 08:30:20

标签: android imageview

我正在尝试使用Android中的ImageView显示图像。图像数据通过Web服务来自数据库。在android屏幕上成功显示除image之外的所有其他值。我尝试了以下代码:

Bitmap bm=BitmapFactory.decodeStream((InputStream)response.getProperty(7));
image.setImageBitmap(bm);

但是我得到了类强制转换异常:

12-30 12:51:52.241: ERROR/AndroidRuntime(310): FATAL EXCEPTION: main
12-30 12:51:52.241: ERROR/AndroidRuntime(310): java.lang.ClassCastException: org.ksoap2.serialization.SoapPrimitive
12-30 12:51:52.241: ERROR/AndroidRuntime(310):     at com.trueVUE.modules.report.MainSimulation.onClick(MainSimulation.java:131)
12-30 12:51:52.241: ERROR/AndroidRuntime(310):     at android.view.View.performClick(View.java:2408)
12-30 12:51:52.241: ERROR/AndroidRuntime(310):     at android.view.View$PerformClick.run(View.java:8816)
12-30 12:51:52.241: ERROR/AndroidRuntime(310):     at android.os.Handler.handleCallback(Handler.java:587)
12-30 12:51:52.241: ERROR/AndroidRuntime(310):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-30 12:51:52.241: ERROR/AndroidRuntime(310):     at android.os.Looper.loop(Looper.java:123)
12-30 12:51:52.241: ERROR/AndroidRuntime(310):     at android.app.ActivityThread.main(ActivityThread.java:4627)
12-30 12:51:52.241: ERROR/AndroidRuntime(310):     at java.lang.reflect.Method.invokeNative(Native Method)
12-30 12:51:52.241: ERROR/AndroidRuntime(310):     at java.lang.reflect.Method.invoke(Method.java:521)
12-30 12:51:52.241: ERROR/AndroidRuntime(310):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-30 12:51:52.241: ERROR/AndroidRuntime(310):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-30 12:51:52.241: ERROR/AndroidRuntime(310):     at dalvik.system.NativeStart.main(Native Method)

请建议我解决此问题。

谢谢&问候, Rahul Jaiswal

2 个答案:

答案 0 :(得分:0)

最简单,最简单的方法是在webview中显示它,或者使用setImageURI

在图像视图中使用集合

答案 1 :(得分:0)

Bitmap bimage=  getBitmapFromURL(bannerpath);
image.setImageBitmap(bimage);

 public static Bitmap getBitmapFromURL(String src) {
        try {
            Log.e("src",src);
            URL url = new URL(src);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);
            Log.e("Bitmap","returned");
            return myBitmap;
        } catch (IOException e) {
            e.printStackTrace();
            Log.e("Exception",e.getMessage());
            return null;
        }
    }

在androidmanifest.xml中添加此行

<uses-permission android:name="android.permission.INTERNET" />