Android:上传图片

时间:2012-02-16 04:21:27

标签: php android upload

我目前正在尝试通过Android将图像上传到PHP服务器上。以下是代码:

// Android上的代码段

                        bm = BitmapFactory.decodeFile(imagePath); //imagePath is the path of the image in my SD card    
                        ByteArrayOutputStream bao = new ByteArrayOutputStream();
                        bm.compress(Bitmap.CompressFormat.JPEG, 90, bao);//compressing image
                        byte[] ba = bao.toByteArray();
                        String ba1 = Base64.encodeBytes(ba);
                        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                        nameValuePairs.add(new BasicNameValuePair("image",ba1));

                        try{
                            HttpClient client = new DefaultHttpClient();
                            HttpPost post = new HttpPost("http://domain.com/upload_image.php");
                            post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                            HttpResponse res = client.execute(post);
                            HttpEntity entity = res.getEntity();
                            is = entity.getContent();


                        }catch(Exception e){
                            Log.e("log_tag","Error in http connection "+e.toString());
                        }

// PHP服务器上的代码段(upload_image.php)

<?php
$base=$_REQUEST['image'];

// base64 encoded utf-8 string
$binary=base64_decode($base);

// binary, utf-8 bytes
header('Content-Type: bitmap; charset=utf-8');

$file = fopen('test.jpg', 'wb');

fwrite($file, $binary);

fclose($file);
?>

我无法将图像上传到服务器上,其中test.jpg从未显示在服务器上。我从智能手机运行程序,而不是模拟器。

2 个答案:

答案 0 :(得分:0)

// binary, utf-8 bytes
header('Content-Type: bitmap; charset=utf-8');

没有任何效果,你没有将位图输出到浏览器/ httpclient。

$file = fopen('test.jpg', 'wb');

尝试指定测试的完整路径,例如/tmp/test.jpg

答案 1 :(得分:0)

    String  executeMultipartPost(Bitmap bm,String image_name) {
    String resp = null;
    try {  
    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    bm.compress(CompressFormat.JPEG, 75, bos);

    byte[] data = bos.toByteArray();

    HttpClient httpClient = new DefaultHttpClient();

    HttpPost postRequest = new HttpPost("domain.com/upload_image.php");

    ByteArrayBody bab = new ByteArrayBody(data, image_name);

    MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
    reqEntity.addPart("uploaded", bab);
    reqEntity.addPart("photoCaption", new StringBody("sfsdfsdf"));
    postRequest.setEntity(reqEntity);
    HttpResponse response = httpClient.execute(postRequest);
            BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
    String sResponse;
    StringBuilder s = new StringBuilder();
    while ((sResponse = reader.readLine()) != null) {
    s = s.append(sResponse);
    }
    resp=s.toString();
    } catch (Exception e) {
    // handle exception here
    Log.e(e.getClass().getName(), e.getMessage());
    }
    return resp;


    }
<?php 

$target = "upload/"; 

$target = $target . basename( $_FILES['uploaded']['name']) ; 
$ok=1; 
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) 
{
echo "yes";
} 
else {
echo "no";
}
?>