android从URL显示图像时出现错误

当我使用以下代码从url显示图像时,应用程序花费太多时间从服务器加载图像.如果要加载8-10张图像,则应用程序挂起.

Bitmap mbmp = BitmapFactory.decodeStream(new java.net.URL("urlname").openStream());
Imageview_ref.setImageBitmap(mbmp);

并在Logcat中获取

09-15 11:34:14.265: ERROR/Cursor(14443): Finalizing a Cursor that has not been deactivated or closed. database = /data/data/com.UserLogin/databases/sample.db, table = null, query = SELECT LocationName , LocationImage FROM Locationlist
09-15 11:34:14.265: ERROR/Cursor(14443): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
09-15 11:34:14.265: ERROR/Cursor(14443):     at android.database.sqlite.SQLiteCursor.<init>(SQLiteCursor.java:210)
09-15 11:34:14.265: ERROR/Cursor(14443):     at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:53)
09-15 11:34:14.265: ERROR/Cursor(14443):     at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1345)
09-15 11:34:14.265: ERROR/Cursor(14443):     at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1315)
09-15 11:34:14.265: ERROR/Cursor(14443):     at com.UserLogin.FindPlaces.getallLocs(FindPlaces.java:134)
09-15 11:34:14.265: ERROR/Cursor(14443):     at com.UserLogin.FindPlaces.onCreate(FindPlaces.java:37)
09-15 11:34:14.265: ERROR/Cursor(14443):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-15 11:34:14.265: ERROR/Cursor(14443):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
09-15 11:34:14.265: ERROR/Cursor(14443):     at android.app.ActivityThread.startActivityNow(ActivityThread.java:2503)
09-15 11:34:14.265: ERROR/Cursor(14443):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
09-15 11:34:14.265: ERROR/Cursor(14443):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
09-15 11:34:14.265: ERROR/Cursor(14443):     at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:651)
09-15 11:34:14.265: ERROR/Cursor(14443):     at android.widget.TabHost.setCurrentTab(TabHost.java:323)
09-15 11:34:14.265: ERROR/Cursor(14443):     at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:129)
09-15 11:34:14.265: ERROR/Cursor(14443):     at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:453)
09-15 11:34:14.265: ERROR/Cursor(14443):     at android.view.View.performClick(View.java:2408)
09-15 11:34:14.265: ERROR/Cursor(14443):     at android.view.View$PerformClick.run(View.java:8816)
09-15 11:34:14.265: ERROR/Cursor(14443):     at android.os.Handler.handleCallback(Handler.java:587)
09-15 11:34:14.265: ERROR/Cursor(14443):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-15 11:34:14.265: ERROR/Cursor(14443):     at android.os.Looper.loop(Looper.java:123)
09-15 11:34:14.265: ERROR/Cursor(14443):     at android.app.ActivityThread.main(ActivityThread.java:4627)
09-15 11:34:14.265: ERROR/Cursor(14443):     at java.lang.reflect.Method.invokeNative(Native Method)
09-15 11:34:14.265: ERROR/Cursor(14443):     at java.lang.reflect.Method.invoke(Method.java:521)
09-15 11:34:14.265: ERROR/Cursor(14443):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-15 11:34:14.265: ERROR/Cursor(14443):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-15 11:34:14.265: ERROR/Cursor(14443):     at dalvik.system.NativeStart.main(Native Method)

与此logcat错误消息,我仍然得到输出.

这是我更新的代码视图.

public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            View v = convertView;
            if(v == null){
                LayoutInflater vl = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vl.inflate(R.layout.placeslist, null);
            }
            Fields o = results.get(position);

            if (o != null) {
                TextView iv = (TextView)v.findViewById(R.id.toptext);
                ImageView tv_Image = (ImageView)v.findViewById(R.id.Locimage);

                iv.setText(o.getLocationName());                            

                try {
                    Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(o.getLocationImage()).getContent());
                    tv_Image.setImageBitmap(bitmap);
                } catch (Exception e) {
                    e.printStackTrace();
                }


            }
            return v;
        }       
    }

解决方法:

请尝试这个::

            try {
        Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(
                "http://xyz.com/a/f/a.png")
                .getContent());
        tran_btn_skip.setImageBitmap(bitmap);
    } catch (Exception e) {
    }

确保您必须授予Internet许可

上一篇:Android Studio 之 ImageView 学习笔记


下一篇:android-imageView.setImageBitmap(Bitmap)极大地减慢了我的应用程序