2011-09-14 121 views
-1

如何從圖像視圖中的圖像url中獲取圖像。
我的imageUrl來自databaseadapter。

In Fields class LocationImage dataype是字符串,但是在setBackgroundResource它要求int值作爲參數的方法。 LocationImage url從數據庫中獲取,所以我把它當作字符串變量。
如何從imageUrl中獲取圖像imageView

代碼行在這裏。

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"); 
     } 
       } 
} 
+1

這像在SD卡或資源文件夾一樣繪製文件夾中?可以告訴我從數據庫中檢索的路徑嗎? – Pratik

+0

你在ur.getLocationImage()中獲得了哪些代碼.is url或lese – Pinki

+0

url就像[link](http://www.gravatar.com/avatar/f80674989557a35b0fec1fdc21ecbcc4?s=32&d=identicon&r=PG) M存儲圖像網址的數據庫,在顯示屏中,我想直接從網址顯示..這是正確的方式做? – atluriajith

回答

2

如果你長了圖像URL中使用下面的代碼從URL中的圖像設置爲ImageView的

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

能否請您在我的應用程序中顯示圖像的最佳方式。我從數據庫中獲取圖像網址,我想顯示圖像。爲此,我需要訪問互聯網權利?這是正確的方式嗎? – atluriajith

+0

@atluriajith:你需要互聯網連接,並在清單文件中使用權限,並按照上述代碼 – Pinki

+0

如果我們使用互聯網加載每個圖像。然後它會消耗電池。 – atluriajith

相關問題