android 从 MySql 数据库中检索 blob 图像

Posted

技术标签:

【中文标题】android 从 MySql 数据库中检索 blob 图像【英文标题】:android retrieve blob images from MySql database 【发布时间】:2014-02-19 06:47:33 【问题描述】:

我正在尝试使用包含术语的列表视图开发医学词典,并且当单击项目时,详细信息会出现在另一个意图上。所有这些都是从我的本地数据库中获取的,我不使用 SQLITE。我希望医学的每个细节都至少有一个图像。但我只能检索文本而不能检索图像。

谁知道怎么做?

这是我显示药物详细信息的代码。

MedInfoActivity.java

import library.JSONParser2;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


import android.app.Activity;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;


public class MedInfoActivity extends Activity 

    TextView txtMedInfo;
    TextView med_name;
    String mid;
    ImageView medimg;

    // Progress Dialog
    private ProgressDialog pDialog;

    // JSON parser class
    JSONParser2 jsonParser = new JSONParser2();

    // url to get all products list
    private static String url_med_info = "http://10.0.2.2/android_connect/med_info.php";

    // JSON Node names
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_MEDICINE = "medicine";
    private static final String TAG_MED_ID = "mid";
    private static final String TAG_MED_NAME = "med_name";
    private static final String TAG_MED_INFO = "med_info";
    private static final String TAG_IMAGE = "image";


    @Override
    public void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.med_info);

        // save button
        //btnSave = (Button) findViewById(R.id.btnSave);
        //btnDelete = (Button) findViewById(R.id.btnDelete);

        // getting product details from intent
        Intent i = getIntent();

        // getting product id (pid) from intent
        mid = i.getStringExtra(TAG_MED_ID);

        // Getting complete product details in background thread
        new GetMedicineDetails().execute();

    

    /**
     * Background Async Task to Get complete product details
     * */
    class GetMedicineDetails extends AsyncTask<String, String, String> 

        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() 
            super.onPreExecute();
            pDialog = new ProgressDialog(MedInfoActivity.this);
            pDialog.setMessage("Loading medicines details. Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        

        /**
         * Getting product details in background thread
         * */
        protected String doInBackground(String... params) 

            // updating UI from Background Thread
            runOnUiThread(new Runnable() 
                public void run() 
                    // Check for success tag
                    int success;
                    try 
                        // Building Parameters
                        List<NameValuePair> params = new ArrayList<NameValuePair>();
                        params.add(new BasicNameValuePair("mid", mid));

                        // getting product details by making HTTP request
                        // Note that product details url will use GET request
                        JSONObject json = jsonParser.makeHttpRequest(
                                url_med_info, "GET", params);

                        // check your log for json response
                            Log.d("Single Medicine Details", json.toString());

                            // json success tag
                            success = json.getInt(TAG_SUCCESS);
                            if (success == 1) 
                                // successfully received product details
                                JSONArray medObj = json
                                        .getJSONArray(TAG_MEDICINE); // JSON Array

                                // get first product object from JSON Array
                                JSONObject medical_dictionary = medObj.getJSONObject(0);

                                // product with this pid found
                                // Edit Text
                                med_name = (TextView) findViewById(R.id.med_name);
                                txtMedInfo = (TextView) findViewById(R.id.inputMedInfo);
                                medimg = (ImageView) findViewById(R.id.medimg);



                                // display product data in EditText
                                txtMedInfo.setText(medical_dictionary.getString(TAG_MED_INFO));
                                med_name.setText(medical_dictionary.getString(TAG_MED_NAME));
                                medimg.setTag(medical_dictionary.getString(TAG_IMAGE));


                            else
                                // product with pid not found
                            
                         catch (JSONException e) 
                            e.printStackTrace();
                        
                    
                );

                return null;
            


            /**
             * After completing background task Dismiss the progress dialog
             * **/
            protected void onPostExecute(String file_url) 
                // dismiss the dialog once got all details
                pDialog.dismiss();
            
        

med_info.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_
    android:layout_
    android:background="@drawable/white_bg"
    android:orientation="vertical" >

    <!-- Name Label -->

    <TextView
        android:id="@id/med_name"
        android:layout_
        android:layout_
        android:paddingLeft="10dip"
        android:paddingRight="10dip"
        android:paddingTop="10dip"
        android:textColor="#000000"
        android:textSize="30dp" />

    <!-- Input description -->

    <ImageView
        android:id="@+id/medimg"
        android:layout_
        android:layout_ />

    <TextView
        android:id="@+id/inputMedInfo"
        android:layout_
        android:layout_
        android:layout_margin="5dip"
        android:layout_marginBottom="15dip"
        android:gravity="top"
        android:lines="28"
        android:textColor="#000000"
        android:textSize="19dp" />

</LinearLayout>

谁能告诉我如何使用我的代码添加代码以从表 Medicine_dictionary 中检索图像并检索列 med_name、med_info 包括图像。请。我会非常感激并感谢那些会回答我的人。

【问题讨论】:

【参考方案1】:

你需要这样设置,在你的onPostExe方法中,这里的结果是一个位图图像,你需要转换你的

 medical_dictionary.getString(TAG_IMAGE);

放入位图,然后将其传递到结果中,

   medimg.setImageBitmap(result);

【讨论】:

以上是关于android 从 MySql 数据库中检索 blob 图像的主要内容,如果未能解决你的问题,请参考以下文章

Android:无法从mysql数据库中检索图像URL并将其显示在imageView中

Android AsyncTask 无法从 php-mysql 检索 JSON 字符串

Android:如何让我检索到的(来自 mysql)JSON 解析数据添加到 ListView 每分钟刷新一次

将从数据库中检索的值设置为字符串到android中的评分栏

使用php从mysql获取数据到android

从MySql DB检索PDF到android(使用Volley)