我正在使用相机意图捕获图像,然后将图像上传到 php Web 服务器。没有生成错误,但是当我查看图像的目标文件夹时,没有任何错误。这是代码:
//for camera intent
addphoto.setOnClickListener(new OnClickListener() {
//LAUNCH CAMERA
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent cameraintnent=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraintnent, 1);
}
});
然后这是 onActivityResult() 方法代码
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==1) {
//for camera
bitmapOrg= (Bitmap) data.getExtras().get("data");
String outPut = null;
ByteArrayOutputStream bao = new ByteArrayOutputStream();
//Resize the image
double width = bitmapOrg.getWidth();
double height = bitmapOrg.getHeight();
double ratio = 400/width;
int newheight = (int)(ratio*height);
bitmapOrg = Bitmap.createScaledBitmap(bitmapOrg, 400, newheight, true);
bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 95, 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://localhost/m/upload.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
Log.d("image uploading","The image is uploading");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
// print response
outPut = EntityUtils.toString(entity);
Log.i("GET RESPONSE—-", outPut);
//is is the InputStream declared globally within the class
is = entity.getContent();
Log.e("log_tag ******", "good connection");
bitmapOrg.recycle();
} catch (Exception e) {
Log.e("log_tag ******", "Error in http connection " + e.toString());
}
image.setImageBitmap(bitmapOrg);
setContentView(parentContainer);
}
这是php代码:
<?php
$base = $_REQUEST["image"];
if (isset($base)) {
$suffix = createRandomID();
$image_name = "img_".$suffix."_".date("Y-m-d-H-m-s").".jpg";
// base64 encoded utf-8 string
$binary = base64_decode($base);
// binary, utf-8 bytes
header("Content-Type: bitmap; charset=utf-8");
$file = fopen("../images/post_images/" . $image_name, "wb");
fwrite($file, $binary);
fclose($file);
die($image_name);
} else {
die("No POST");
}
function createRandomID() {
$chars = "abcdefghijkmnopqrstuvwxyz0123456789?";
//srand((double) microtime() * 1000000);
$i = 0;
$pass = "";
while ($i <= 5) {
$num = rand() % 33;
$tmp = substr($chars, $num, 1);
$pass = $pass . $tmp;
$i++;
}
return $pass;
}
?>
问题出在哪里以及如何解决?