java - 从 android 中的 longBlob 数据创建位图

标签 java php android mysql bitmap

我正在尝试检索在我的服务器上的 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); 

关于java - 从 android 中的 longBlob 数据创建位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28646443/

相关文章:

java - Vertx http 服务器仅创建一个实例

android - RecyclerView Adapter 和 ViewHolder 动态更新

Android 工具栏和 SlidingTabLayout 大小

Android Spinner 是由魔法触发的

php - 使用 jQuery 为每个元素运行 .post()

php - Codeigniter 安全 - mysql 数据库

java - 如何递归计算数组中负数的数量(Java)?

java - 插入不能正常工作

java - 使用 SimpleDateFormat 将日期时间值转换为预期值

php - 写入文件的输入可以被恶意篡改吗?