Php 从mysql数据库检索blob图像并在android listview中显示
我已经在phpmyadmin中创建了一个名为article的数据库,其中包含有关这些文章的信息和存储为blob的图像。我能够从数据库中获取数据并在android列表视图中显示它,但我不知道如何从数据库中检索blob图像并在android列表视图中显示它以及相应的文章信息。。请帮帮我。。我用谷歌搜索了很多次,但都没能做到。。 任何到教程的链接或任何张贴的代码都将非常丰富。。。我的项目需要这个。。 提前感谢: 我的php代码是-Php 从mysql数据库检索blob图像并在android listview中显示,php,android,database,json,Php,Android,Database,Json,我已经在phpmyadmin中创建了一个名为article的数据库,其中包含有关这些文章的信息和存储为blob的图像。我能够从数据库中获取数据并在android列表视图中显示它,但我不知道如何从数据库中检索blob图像并在android列表视图中显示它以及相应的文章信息。。请帮帮我。。我用谷歌搜索了很多次,但都没能做到。。 任何到教程的链接或任何张贴的代码都将非常丰富。。。我的项目需要这个。。 提前感谢: 我的php代码是- <?php header('Content-type: appl
<?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代码是-
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);
}
});
}
}
这不起作用:你能告诉我我犯了什么错误吗???如果你正在从数据库中检索图像,不管你想用哪种设备,图像就是图像 您可以执行以下操作
$im = imagecreatefromstring($imageContent);
if ($im !== false) {
header('Content-Type: image/png'); // Change to what you want
imagepng($im);
imagedestroy($im);
}
else {
echo 'An error occurred.';
}
也许您可以将图像存储在文件夹中 并将文件夹链接存储在数据库中,然后通过链接检索图像
谢谢你的时间:我对php代码很在行。。我需要知道如何编写检索blob图像的java代码:
try
{
Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(imageUrl).getContent());
imageView.setImageBitmap(bitmap);
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}