[英]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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.