我使用了一些来自互联网的代码来上传图片
public class ActUpload extends Activity {
InputStream is;
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
R.drawable.blue);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 90, bao);
byte [] ba = bao.toByteArray();
String ba1=Base64.encodeBytes(ba);
ArrayList<NameValuePair> nameValuePairs = new
ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("image",ba1));
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new
HttpPost("http://127.0.0.1:80/php/base.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
}
}
甚至拿了一些PHP代码复制了一些我使用wamp服务器在本地服务器上运行的php代码。本地服务器中没有响应。
这是我的PHP代码。
<?php
$base=$_REQUEST['image'];
echo $base;
// base64 encoded utf-8 string
$binary=base64_decode($base);
// binary, utf-8 bytes
header('Content-Type: bitmap; charset=utf-8');
// print($binary);
//$theFile = base64_decode($image_data);
$file = fopen('test.jpg', 'wb');
fwrite($file, $binary);
fclose($file);
echo '<img src=test.jpg>';
?>
任何人都可以帮助我。提前谢谢。
答案 0 :(得分:0)
将图像上传到服务器的好教程 -
http://coderzheaven.com/2011/04/android-upload-an-image-to-a-server/
修改强>
您只需根据本地服务器进行更改:
HttpPost httppost = new HttpPost("http://<local ip address>/android/upload_image.php");
// For example,in my code,i used:
// HttpPost httppost = new HttpPost("http://192.168.100.47/android/upload_image.php");
答案 1 :(得分:0)
你设法解决了这个问题吗?当我更改$ base = $ _ REQUEST ['image']时,我确实让它工作了; =&GT; $基地= $ _ POST [ '图像'];并注释掉以下行标题('Content-Type:bitmap; charset = utf-8');顺便说一句,确实尝试将代码转换为使用HttpURLConnection api?