问题描述
我已经包含有关的文章和图片(存储为BLOB)信息命名文章的phpmyadmin创建了一个数据库。我为能够从数据库获取数据,并在android列表视图中显示,但即时通讯没有得到如何从数据库中检索一个blob的图像,并与Android的列表视图中的相应文章信息一起显示出来..请帮助我..我没有使用Google的很多,但我没有做到..
任何链接教程或贴会很greatfull任何code ...我需要这为我的项目..
在此先感谢:)
I have created a database in phpmyadmin named article that contains information regarding the articles and an image(stored as blob). I m able to fetch the data from the data base and display it in the android list view but i m not getting how to retrieve a blob image from the database and display it along with the corresponding article information in android list view.. please help me.. i did lot of googling but i failed to do..Any link to tutorial or any code posted will be very greatfull... i need this for my project..Thanks in advance :)
我的PHP code是 -
My php code is -
<?php
header('Content-type: application/json');
mysql_query('SET CHARACTER SET utf8');
mysql_connect("localhost","root","");
mysql_select_db("reader");
$id=$_REQUEST['keyword'];
$id1=(int)$id;
$sql=mysql_query("SELECT * FROM article a WHERE a.a_id='{$id}'");
while($row=mysql_fetch_assoc($sql))
{
$row['a_thumbnail']=base64_encode($row['a_thumbnail']);
$row1= array_slice($row, 0, 2);
$row_slice=array_slice($row,2);
$output2=array_map('utf8_encode', $row_slice);
$output[]=array_merge((array)$row1, (array)$output2);
}
print(json_encode($output));
mysql_close();
?>
化java code我写来获取图像 -
The java code i wrote to retrieve image is-
public class SearchByLikeCount extends ListActivity {
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
String result = "";
InputStream is= null;
try{
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://10.0.2.2/likeCountsearch.php");
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
try{
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++){
HashMap<String,String> map = new HashMap<String, String>();
JSONObject e = jArray.getJSONObject(i);
Log.i("shruthi", "jason object length = " + e.length());
map.put("id", e.getString("a_id"));
map.put("title", e.getString("a_title"));
map.put("author", e.getString("a_author"));
map.put("image", e.getString("a_thumbnail"));
mylist.add(map);
}
}catch(JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
ListAdapter adapter = new SimpleAdapter(SearchByLikeCount.this, mylist , R.layout.main2,
new String[] { "title", "author","image"},
new int[] { R.id.item_title ,R.id.item_author,R.id.list_image});
setListAdapter(adapter);
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
@SuppressWarnings("unchecked")
HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);
Object v=o.get("id");
String id1=v.toString();
Intent myintent= new Intent(SearchByLikeCount.this,ViewArticle.class);
myintent.putExtra("articleId", id1);
startActivity(myintent);
}
});
}
}
这没有工作:(可以在任何分不清什么是错误,我做了??
This didnt work :( can any tell what was the mistake i did??
推荐答案
也许你可以存储在文件夹中的形象
结果
并存储在数据库中的文件夹链接,并通过您的链接检索图像
结果
Maybe you can store the image in folders
and store the folder link in your database, and retrieve the image by your link
try
{
Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(imageUrl).getContent());
imageView.setImageBitmap(bitmap);
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
这篇关于从MySQL数据库中检索BLOB图像和Android的ListView中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!