Skip to content Skip to sidebar Skip to footer

Android: Uploading Image

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.decodeFi

Solution 1:

// binary, utf-8 bytesheader('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.

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

Post a Comment for "Android: Uploading Image"