我是Android的初学者,我想将图像上传到我的服务器,但问题是图像上传但我无法正常查看,但有一些问题,但我不知道在哪里......所以请帮助我。
这是我将代码上传到服务器的后台活动的安卓代码
public class BackgroundSendActivity extends AsyncTask<Void, Void, String> {
Bitmap ori_image;
String ID, send_url, image;
BackgroundSendActivity(Bitmap ori_image, String ID) {
this.ori_image = ori_image;
this.ID = ID;
}
@Override
protected void onPreExecute() {
send_url = "http://androidmedinfo.net23.net/sendtoserver.php";
}
@Override
protected String doInBackground(Void... params) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ori_image.compress(Bitmap.CompressFormat.JPEG,100, byteArrayOutputStream);
image = Base64.encodeToString(byteArrayOutputStream.toByteArray(), Base64.DEFAULT);
try {
URL url = new URL(send_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String data = URLEncoder.encode("ID", "UTF-8") + "=" + URLEncoder.encode(ID, "UTF-8") + "&" +
URLEncoder.encode("image", "UTF-8") + "=" + URLEncoder.encode(image, "UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
inputStream.close();
httpURLConnection.disconnect();
return "Image Uploaded Successfully....";
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "Error while uploading image.....";
}
这是我上传图片的php代码
<?php
$id = urldecode($_POST["ID"]);
$image = urldecode($_POST["image"]);
$decodeImage = base64_decode("$image");
file_put_contents("pictures/".$id.".jpg",$decodeImage);
?>