Android的:如何从sqlite的名字动态加载图像从服务器 [英] Android :How to load image dynamically from server by its name from SQlite
问题描述
我是新来的Android和我现在面临的问题,同时从服务器显示基于图像的sqlite的名字
例如: 我只存储映像名称(文本)在SQLite数据库(列名的图像),我想从加载基于SQLite的图像服务器的图像命名图像要在ImageView的显示
在服务器上创建一个文件夹,如汽车该文件夹中保存我的图像与汽车names..but SQLite中我只需添加一个carname文本格式与JPEG格式
我在我的DB两个列名:
- 第一是车名
- 二是车详细信息
当用户选择的车名,在接下来的活动中,用户可以看到车的详细信息与图像。 我在这里展示的细节,但我不知道如何展示汽车图片从服务器
感谢
下面是我的code:
@覆盖
保护无效的onCreate(包savedInstanceState)
{
super.onCreate(savedInstanceState);
的setContentView(R.layout.detail_activity);
detailtext =(TextView中)findViewById(R.id.detail);
ImageView的=(ImageView的)findViewById(R.id.images);
意向意图= getIntent();
最后弦乐selectedData = intent.getStringExtra(selectedItem设置);
actionBar.setTitle(selectedData);
dbHelper =新SqlLiteDbHelper(本);
尝试 {
dbHelper.openDataBase();
}赶上(的SQLException E){
e.printStackTrace();
}
sqLiteDatabase = dbHelper.getReadableDatabase();
光标= dbHelper.getdetails(sqLiteDatabase,selectedData);
如果(cursor.moveToFirst()){
detailtext.setText(cursor.getString(0));
串imagename = cursor.getString(1);
字符串IMAGEURL =HTTP://您的服务器/车/+ imagename;
Picasso.with(本).load(IMAGEURL)。走进(ImageView的);
}
注:图片现场就是四纵 http://i.stack.imgur.com/ lqvOQ.png 而在服务器我把图像www.server.com/cars/carnames.jpg
在sqlite的,我只是将图像粘贴名称以.jpg例如:carnames.jpg
SqliteDbHelper
公开光标getdetails(SQLiteDatabase分贝,串张图片)
{
DB = this.getReadableDatabase();
光标光标;
光标= db.query(记录,新的String [] {详细信息,图像,ITEMNO +作为_id},SUBCATEGORY +=+ IMG +',NULL,NULL,NULL,NULL);
返回游标;
}
如果您要加载来自SQLite的图像话,我建议你保存文件/文件位置,如果数据被存储在本地,为单个图像上的表现所有图像视图那是因为你只加载一个单一的形象,你可能想要把所有的IDS在数组中,并把它传递给数据库,该数据库查询还应该回到它应该加载到图像位置阵列/数组列表使用循环
比如我有一个加载了一堆照片从我SQLite数据库的查询你的形象的看法,这显示某一类子类别的图像命名的球鞋,所以我们有智能鞋图片
,休闲鞋
等我传递一个ID作为参数
公开的ArrayList< CategoryItem> getAllSubCategories(INT mtargetID)抛出的SQLException {
ArrayList的< CategoryItem> myshoes =新的ArrayList<>();
//选择所有查询
串sQuery =SELECT+ Constant.CATEGORY_TB_ID +,+ Constant.SUB_DESCRIPTION +
,+ Constant.SUB_IMAGEPATH +FROM+ Constant.CATEGORY_TABLE +
INNER JOIN+ Constant.SUB_CATEGORY_TABLE +ON+ Constant.CATEGORY_TB_ID +=+ Constant.SUB_CATEGORY_FK
+WHERE+ Constant.CATEGORY_TB_ID +=?;
字串[] args =新的String [1];
的args [0] =将String.valueOf(mtargetID);
SQLiteDatabase DB = this.getWritableDatabase();
光标光标= db.rawQuery(sQuery,参数);
//遍历所有行和添加到列表
如果(cursor.moveToFirst()){
做 {
CategoryItem myShoesItem =新CategoryItem();
//我的外部存储鞋图片路径
myShoeItem.setmCategoryImgPath(cursor.getString(2));
//添加我的图像路径,以我的数组列表
myshoes.add(myShoeItem);
}而(cursor.moveToNext());
}
//返回我的ArrayList显示到我的ImageView
返回mshoes;
}
在接收端通过我araylist然后横向
的for(int i = 0; I< getAllSubCategories.size();我++)
{
imageView.setImageUri(getAllSubCategories.get(ⅰ).getmCategoryImgPath())
}
通过这种方法,你将图片设置为所有imageviews。
I am new to android and I'm facing an issue while displaying image from server based its name from Sqlite
ie: I stored only image name (text) in SQLite database (column name images) and I want to load images from Server based on the sqlite image name the image want to display in imageview
In server I create a folder like Cars in that folder I store images with car names..but in sqlite I just add a carname in text format with .jpeg
I have two column names in my DB:
- first is Car name
- Second is Car Detail
When user selects the Car name, in next activity the user can see the Car details with images. Here I display Details, But I don't know How to display Car Images From Server
Thanks
Here is my Code:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.detail_activity);
detailtext= (TextView) findViewById(R.id.detail);
imageView= (ImageView) findViewById(R.id.images);
Intent intent= getIntent();
final String selectedData = intent.getStringExtra("selectedItem");
actionBar.setTitle(selectedData);
dbHelper = new SqlLiteDbHelper(this);
try {
dbHelper.openDataBase();
} catch (SQLException e) {
e.printStackTrace();
}
sqLiteDatabase = dbHelper.getReadableDatabase();
cursor=dbHelper.getdetails(sqLiteDatabase, selectedData);
if (cursor.moveToFirst()) {
detailtext.setText(cursor.getString(0));
String imagename = cursor.getString(1);
String imageUrl = "http://your_server/car/" + imagename ;
Picasso.with(this).load(imageUrl).into(imageView );
}
Note:Image Field is the 4th Column http://i.stack.imgur.com/lqvOQ.png and in server i put image in www.server.com/cars/carnames.jpg
in sqlite i just paste the image name with .jpg ex:carnames.jpg
SqliteDbHelper
public Cursor getdetails(SQLiteDatabase db,String img)
{
db=this.getReadableDatabase();
Cursor cursor;
cursor=db.query("record",new String[]{DETAIL,IMAGES,ITEMNO + " as _id"},SUBCATEGORY + "='" +img+"'",null,null,null,null);
return cursor;
}
If you want to load images from SQlite then I suggest you save the file/file location if the data is stored locally, for the showing of a single image on all image view it is because you are only loading a single image, you might want to put all your ids in an array and passing it to the db, the db query should also return an array/arraylist of image locations which you should load into your image views using a for loop
e.g I have a query that loads a bunch of images from my SQLite database, this displays sub-category images of a certain category named shoes so we have images of smart shoes
, Casual shoes
and more I pass an an Id as parameter
public ArrayList<CategoryItem> getAllSubCategories(int mtargetID) throws SQLException{
ArrayList<CategoryItem> myshoes = new ArrayList<>();
// Select All Query
String sQuery = " SELECT "+Constant.CATEGORY_TB_ID+", "+Constant.SUB_DESCRIPTION+
", "+Constant.SUB_IMAGEPATH+" FROM "+Constant.CATEGORY_TABLE+
" INNER JOIN "+Constant.SUB_CATEGORY_TABLE+" ON "+Constant.CATEGORY_TB_ID +" = " +Constant.SUB_CATEGORY_FK
+ " WHERE "+Constant.CATEGORY_TB_ID +" = ?";
String[] args = new String[1];
args[0] = String.valueOf(mtargetID);
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(sQuery, args);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
CategoryItem myShoesItem = new CategoryItem();
//my shoe image path on external storage
myShoeItem.setmCategoryImgPath(cursor.getString(2));
//i add my image paths to my array list
myshoes.add(myShoeItem);
} while (cursor.moveToNext());
}
// return my arraylist for display into my imageview
return mshoes;
}
on the receiving side I then transverse through my araylist
for(int i = 0; i <getAllSubCategories.size(); i++ )
{
imageView.setImageUri(getAllSubCategories.get(i).getmCategoryImgPath())
}
with this method you will set images to all your imageviews.
这篇关于Android的:如何从sqlite的名字动态加载图像从服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!