2012-04-21 179 views
1

我在phpmyadmin命名的文章中創建了一個數據庫,其中包含有關文章和圖像(存儲爲blob)的信息。我能夠從數據庫中獲取數據並將其顯示在android列表視圖中,但我沒有得到如何從數據庫中檢索blob圖像,並在android list view中顯示它以及相應的文章信息..請幫助我..我做了很多谷歌搜索,但我沒有做.. 教程或任何代碼發佈的任何鏈接將是非常greatfull ...我需要這個爲我的項目.. 在此先感謝:)從mysql數據庫中檢索blob圖像並在android listview中顯示

我的PHP代碼 -

<?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); 
     } 
    }); 
} 
} 

這沒有工作:(可以告訴我做錯了什麼?

回答

0

如果從數據庫中檢索圖像也不要緊,你想用它的圖像哪個設備的圖像

你可以做以下

$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.'; 
} 
+0

感謝您的時間:)我很好用PHP代碼..我需要知道如何編寫檢索blob圖像的java代碼:) – Shree 2012-04-21 16:11:34

0

也許你可以存儲在圖像文件夾
並存儲在數據庫中的文件夾的鏈接,並通過您的鏈接

try 
{ 
    Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(imageUrl).getContent()); 
    imageView.setImageBitmap(bitmap); 
} 
catch (MalformedURLException e) 
{ 
     e.printStackTrace(); 
} 
catch (IOException e) 
{ 
     e.printStackTrace(); 
} 
檢索圖像
相關問題