我正在尝试在我的服务器上检索在Mysql中存储为Blob的图像,并在Android中创建Bitmap。 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();
}
}
答案 0 :(得分:0)
返回的数据似乎是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);