从 android 中的 longBlob 数据创建位图

Create Bitmap from longBlob data in android

我正在尝试检索在我的服务器上 Mysql 中存储为 Blob 的图像,并在 Android 中创建位图。 php代码:

<?php
require 'db_connect.php';
$db = new DB_CONNECT();
 if (isset($_POST["id"])) {
    $id = $_POST['id'];

    $result = mysql_query("SELECT * FROM max_form WHERE id = $id");

    $result;

    if (!empty($result)) {
        $row = mysql_fetch_array($result);      
        $result = base64_encode($row["bin_data"]);
    } else      
        $result = "fail";
}
else
    $result = "missing";

echo $result;
?>

返回数据: /9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAUDBAQEAwUEBAQFBQUGBwwIBwcHBw8LCwkMEQ8SEhEPERET...等 但是 BitmapFactory.decodeStream(inputStream) 返回 null。 JAVA代码:

class RetrievePhotos extends AsyncTask<String, String, Void> {

        InputStream inputStream = null;

        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(GeneralData.context);
            pDialog.setMessage("Checking customer. Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        @Override
        protected Void doInBackground(String... params) {

            ArrayList<NameValuePair> param = new ArrayList<NameValuePair>();
            try {
                param.add(new BasicNameValuePair("id", "7"));
                HttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);
                httpPost.setEntity(new UrlEncodedFormEntity(param));
                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();

                inputStream = httpEntity.getContent();
                bmp = BitmapFactory.decodeStream(inputStream);//bmp = null
                try {
                    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"), 8);
                    StringBuilder sb = new StringBuilder();
                    String line = null;


       while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }

                inputStream.close();
                Log.d(GeneralData.TAG_LOG, "result =" + sb.toString());

            } catch (Exception e) {
                Log.d(GeneralData.TAG_LOG, "Error converting result " + e.toString());
            }
        } catch (UnsupportedEncodingException e1) {
            Log.d(GeneralData.TAG_LOG, e1.toString());
            e1.printStackTrace();
        } catch (ClientProtocolException e2) {
            Log.d(GeneralData.TAG_LOG, e2.toString());
            e2.printStackTrace();
        } catch (IllegalStateException e3) {
            Log.d(GeneralData.TAG_LOG, e3.toString());
            e3.printStackTrace();
        } catch (IOException e4) {
            Log.d(GeneralData.TAG_LOG, e4.toString());
            e4.printStackTrace();
        }
        return null;

    }

    protected void onPostExecute(String file_url) {
        pDialog.dismiss();
    }
}

返回的数据似乎是Base64编码的。要转换位图中的 Base64 字符串,可以使用 BitmapFactory.decodeByteArray。例如,在您的情况下:

HttpEntity httpEntity = httpResponse.getEntity();
String base64String = EntityUtils.toString(httpEntity);
byte[] decodedString = Base64.decode(base64String, Base64.DEFAULT);
Bitmap bmp = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);