【问题标题】:How do i get image from imageUrl in imageView如何从 imageView 中的 imageUrl 获取图像
【发布时间】:2011-11-17 00:56:39
【问题描述】:

如何在图像视图中从图像 url 获取图像。
我的 imageUrl 来自 databaseadapter。

在 Fields 类 LocationImage 中,数据类型是字符串,但在 setBackgroundResource 方法中,它要求输入 int 值作为参数。 LocationImage url 是从数据库中获取的,因此我将其作为字符串变量。

代码行在这里。

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class FindPlaces extends ListActivity{

    private SQLiteDatabase DbLoc;
    ListView lv;
    int val;
    private ArrayList<Fields> results = new ArrayList<Fields>();
    @Override
    public void onCreate(Bundle savedInstance) {
        super.onCreate(savedInstance);
        setContentView(R.layout.places);
        getallLocs();
        setListAdapter(new StudentListAdapter(this,  val, results));
    }

    class StudentListAdapter extends ArrayAdapter<Fields>{
        private ArrayList<Fields> locationDetails;
        private Context mContext;

        public StudentListAdapter(Context context,int textViewResourceId, ArrayList<Fields> results) {
            super(context, textViewResourceId, results);
            // TODO Auto-generated constructor stub
            System.out.println("Constructor StudentList Adapter...");
            this.locationDetails = results;
            mContext = context;
        }
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return results.size();
        }
        @Override
        public Fields getItem(int position) {
            // TODO Auto-generated method stub
            return locationDetails.get(position);
        }
        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return super.getItemId(position);
        }
        @Override
        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);
                TextView tv_sNo = (TextView)v.findViewById(R.id.toptext1);
                ImageView tv_Image = (ImageView)v.findViewById(R.id.Locimage);

                iv.setText(o.getLocationName());                            
                //tv_sNo.setText("Status: "+ o.getOrderStatus());
                tv_sNo.setText(o.getLocationImage());   
                tv_Image.setBackgroundResource(o.getLocationImage());
            }
            DbLoc.close();
                            return v;
        }       
    }
    static class ViewHolder
    {
        TextView Locationname;
        ImageView Locationimage;
    }
    private void getallLocs() {
        // TODO Auto-generated method stub
        try {
            DatabaseHelper dbHelper = new DatabaseHelper(
                    this.getApplicationContext());
            DbLoc = dbHelper.getWritableDatabase();
            Cursor c = DbLoc.rawQuery("SELECT " + DatabaseHelper.LocationName+ " , " + DatabaseHelper.LocationImage + " FROM "
                    + DatabaseHelper.LOCATIONTABLE , null);
            System.out.println("SELECT " + DatabaseHelper.LocationLang+" , "+DatabaseHelper.LocationLat+" , "+ DatabaseHelper.LocationName
                    + " ," + DatabaseHelper.LocationImage + " FROM "
                    + DatabaseHelper.LOCATIONTABLE );
            if (c != null) {
                if (c.moveToFirst()) {
                    do {
                        String LocationName= c.getString(c.getColumnIndex("LocationName"));
                        String Mobile = c.getString(c
                                .getColumnIndex("LocationImage"));
                        Fields p = new Fields(LocationName, Mobile);
                        results.add(p);

                    } while (c.moveToNext());
                }
            }
        } catch (SQLiteException se) {
            Log.e(getClass().getSimpleName(),
            "Could not create or Open the database");
        } 
                }
}

【问题讨论】:

  • 这张图片是在 sdcard 中还是在资源文件夹中,例如在 drawable 文件夹中?可以告诉我你从数据库中检索的路径吗?
  • 你从你的代码中的m o.getLocationImage()得到了什么。是url还是lese
  • url 就像link 我将图像 url 存储在数据库中,在显示中我想直接从 url 显示.. 这是正确的做法吗?

标签: android sqlite imageview dataadapter


【解决方案1】:

如果你有图片 url,请使用下面的代码将图片从 url 设置为 imageview

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

【讨论】:

  • 您能否建议我在我的应用程序中显示图像的最佳方式。我从数据库中获取图像 url,我想显示图像。为此,我需要访问互联网吗?这是正确的做法吗?
  • @atluriajith: 你需要互联网连接并在清单文件中获取使用权限。然后按照上面的代码进行操作
  • 如果我们使用互联网来加载每张图片。然后它会消耗电池。
  • yes.obviously.But 没有互联网连接你没有显示来自 url 的图像
  • 哦!那么如何使用我的图片 url 来显示图片呢?
猜你喜欢
  • 1970-01-01
  • 2012-02-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多