我在服务器中有一些图像,我想用 php 获取这些图像,调整它们的大小并传递给我的 Android 应用程序。我使用下面的代码来调整图像的大小:
<?php
$image = imagecreatefromjpeg("test.jpg");
$original_image_width = imagesx($image);
$original_image_height = imagesy($image);
$resize_ratio = 0.5;
$resized_height = $original_image_height * $ratio;
$resized_width = $original_image_width* $ratio;
$new_image = imagecreatetruecolor($resized_width, $resized_height);
$resized_image = imagecopyresampled($new_image, $image, 0, 0, 0, 0, $resized_width, $resized_height, $original_image_width, $original_image_height);
?>
之后我不知道如何将 resized_image 传递给 android。我正在使用 JSONObject 和 makeHttpRequest 在我的应用程序和服务器之间传递字符串变量,一切正常。我尝试使用以下代码将 resized_image 转换为 base64 字符串:
<?php
$thumbnail = imagejpeg($resized_image);
$thumbnail_base64 = base64_encode($thumbnail);
?>
并像这样传递它,但我得到“类型 java.lang.String 无法转换为 JSONObject”错误。
将它传递给我的 Android 应用程序的最佳方法是什么?如果有人可以告诉我如何传递 base64 字符串,我可以在 Android 端处理其余的。提前致谢。
编辑:我的 Android 代码如下:
class getAllReports extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ShowAllReportsActivity.this);
pDialog.setMessage(getResources().getString(R.string.getting_reports));
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected String doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
JSONObject json = jParser.makeHttpRequest(url_all_reports,
"GET", params);
Log.d("Reports: ", json.toString());
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
reports = json.getJSONArray(TAG_REPORTS);
for (int i = 0; i < reports.length(); i++) {
JSONObject c = reports.getJSONObject(i);
callNoArrayList.add(c.getString(TAG_CALL_NO));
yearArrayList.add(c.getString(TAG_YEAR));
modelArrayList.add(c.getString(TAG_MODEL));
tumbnailArrayList.add(c.getString(TAG_THUMBNAIL);
}
} else {
Intent i = new Intent(getApplicationContext(),
MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
long delayInMillis = 500;
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
pDialog.dismiss();
}
}, delayInMillis);
runOnUiThread(new Runnable() {
public void run() {
populateList();
}
});
}
}
TAGs 和 ArrayLists 在主类中初始化。
我的 PHP 代码的其余部分:
$db = new DB_CONNECT();
mysql_query('SET CHARACTER SET utf8');
$result = mysql_query("SELECT *FROM reports") or die(mysql_error());
if (mysql_num_rows($result) > 0) {
$response["reports"] = array();
while ($row = mysql_fetch_array($result)) {
$report = array();
$report["call_no"] = $row["call_no"];
$report["year"] = $row["year"];
$report["model"] = $row["model"];
$report["thumbnail"] = $thumbnail_base64;
array_push($response["reports"], $report);
}
$response["success"] = 1;
echo json_encode($response);
} else {
$response["success"] = 0;
$response["message"] = "No reports found";
echo json_encode($response);
}