2011-11-14 56 views
1

在我的應用程序中,我需要在Android中顯示問題以及圖像。我已經在sqlite數據庫中存儲問題並顯示它們。我在資產文件夾中創建了一個文件夾DBimages。現在我所有的圖像都在DBimages文件夾中。如何在sqlite數據庫中存儲圖像並顯示它們?

我創建了兩個類。我能夠顯示來自本地sqlite數據庫的問題和選項,但我無法顯示存儲在assets\DBimages\abc.png中的圖像。

所有圖像的名稱都存儲在sqlite數據庫表中的圖片列中。但是如何顯示與特定問題相關的圖像。請幫助我解決這個問題。

UserBO.java

public String getQuestion() { 
return question; 
} 
public void setQuestion(String question) { 
this.question = question; 
} 
public String getImage() { 
return pictures; 
} 

public void setImage(String pictures) { 
this.pictures = pictures; 
} 

Select.java

ArrayList<UserBO> usersList = new ArrayList<UserBO>(); 
for (int i = 0; i < stringList.size(); i++) { 
ArrayList<String> list = stringList.get(i); 
UserBO user = new UserBO(); 
try { 
user.id = Integer.parseInt(list.get(0)); 
user.name = list.get(2); 
user.question = list.get(3); 
user.option1 = list.get(4); 
user.option2 = list.get(5); 
user.option3 = list.get(6); 
user.option4 = list.get(7); 
user.option5 = list.get(8); 
user.pictures = list.get(9); 
user.answer = (list.get(10)); 
} 
catch (Exception e){} 
public View getView(int position, View convertView, ViewGroup parent){ 
View view = convertView; 
try{ 
if (view == null) { 
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
view = vi.inflate(R.layout.list_item, null); 
} 
final UserBO listItem = mList.get(position);     
if (listItem != null) { 
((TextView) view.findViewById(R.id.tv_question)).setText(listItem.getQuestion()+""); 
((ImageView) view.findViewById(R.id.imageView1)).setImageResource(R.drawable.f); 
((RadioButton) view.findViewById(R.id.option1)).setText(listItem.getOption1()); 
((RadioButton) view.findViewById(R.id.option2)).setText(listItem.getOption2()+""); 
((RadioButton) view.findViewById(R.id.option3)).setText(listItem.getOption3()+""); 
((RadioButton) view.findViewById(R.id.option4)).setText(listItem.getOption4()+""); 
((RadioButton) view.findViewById(R.id.option5)).setText(listItem.getOption5()+""); 
}}catch(Exception e){} 
return view; 

}}

回答

2

一些醜陋的方式,存儲在SQLite數據庫圖像。 只需將圖像存儲在SD卡中,並將這些圖像的相關路徑保存在SQLite數據庫中即可。

In your need, put the images in either drawable folder or in asset folder then store its 
name(with question ID or number also) in SQLite and use it in application. 
+0

嗨感謝看到輸入反應的例子。我將我的圖像保存在可繪製的文件夾中。現在我需要從sqlite數據庫圖像列中獲取img路徑和圖像名稱。你能舉個例子來說明一下這個示例代碼。提前致謝。 – RaagaSudha

+0

如果您要將圖像存儲在可繪製文件夾中,那麼我不認爲您必須將其存儲在數據庫文件中,只需將圖像重命名爲問題ID或名稱,然後直接使用繪圖文件。 – user370305

+0

嗨,你可以請示例代碼?例如, – RaagaSudha

0

我有同樣的問題,我首先要澄清這一點,存儲從圖片庫圖片路徑或URI作爲填充字符串時,可能不會得到好的結果或顯示數據的ListView特定的圖像。您需要將您的圖像作爲BLOB數據類型存儲在sqlite數據庫中,並且需要將圖像作爲字節插入到數據庫中。

在我的情況下,我試圖從畫廊的圖像路徑,但從來沒有從我的工作。 映像路徑只適合您的使用從網站

網址時,可以從這個link

相關問題