Android: Uploading Image

懵懂的女人 提交于 2019-12-12 02:48:15

问题


I am currently trying to upload an image onto the PHP server through Android. Below are the codes:

//segment of codes on 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());
                        }

//segment of codes on PHP server (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);
?>

I've failed to upload the image onto the server, in which a test.jpg never shows up on the server. Im running the program from my smartphone, not emulator.


回答1:


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

doesn't have any effect, you're not outputting the bitmap to the browser/httpclient.

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

try to specify a full path for testing, like /tmp/test.jpg.




回答2:


    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";
}
?> 


来源:https://stackoverflow.com/questions/9305499/android-uploading-image

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!