繁体   English   中英

在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); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM