开发者

Image uploading problem

In my application I'm trying to upload file from built-in android gallery to php server.

I'm getting full path of image e.g. /mnt/sdcard/image.jpg...when i click on image to upload it does not show any exception or error or anything like that....if i print server response code is 200 and response is OK...but image is not getting upload on server.

PLEASE HELP!!!!

here is my code

private void uploadImage(String imagePath2) {
         String serverResponseMessage = null;

         File file2=new File(imagePath2);
         String file=file2.getName().replace("/","");
         Log.i("file path",""+file.toString());
         try
            {
            FileInputStream fileInputStream = new FileInputStream(new File(file));
            /* FileInputStream fileInputStream=new FileInputStream(file);
                bitmap=BitmapFactory.decodeStream(fileInputStream);
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
                byte[] data = baos.toByteArray();*/
        URL url = new URL("http://ufindfish.b4live.com/uploadfile.php");
        connection = (HttpURLConnection) url.openConnection();

        // Allow Inputs & Outputs
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setUseCaches(false);

        // Enable POST method
        connection.setRequestMethod("POST");

        connection.setRequestProperty("Connection", "Keep-Alive");
        connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
         //connection.addRequestProperty("skey",""+key);

        outputStream = new DataOutputStream(connection.getOutputStream());

        outputStream.writeBytes(twoHyphens + boundary + lineEnd);
        outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + file +"\""  + lineEnd);
        outputStream.writeBytes(lineEnd);

        bytesAvailable = fileInputStream.available();
        int maxBufferSize=1000;
       bufferSize = Math.min(bytesAvailable, maxBufferSize);
        buffer = new byte[bufferSize];

        // Read file
        bytesRead = fileInputStream.read(buffer, 0, bufferSize);

        while (bytesRead > 0)
        {
        outputStream.write(buffer, 0, bufferSize);
        bytesAvailable = fileInputStream.available();
        bufferSize = Math.min(bytesAvailable, maxBufferSize);
        bytesRead = fileInputStream.read(buffer, 0, bufferSize);
        }

        outputStream.writeBytes(lineEnd);
        outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

        // Responses from the server (code and message)

        Log.e("debug", "File is written");


        int serverResponseCode = connection.getResponseCode();
        Log.i("Responce code",""+serverResponseCode);
      serverResponseMessage = connection.getResponseMessage();
        Log.i("Responce :",serverResponseMessage);
        fileInputStream.close();
        outputStream.flush();
        outputStream.close();
        }
       catch (Exception e) {
        e.printStackTrace();
    }
       if(serverResponseMessage.equalsIgnoreCase("Ok"))
       {
           Toast.makeText(UploadFromGalleryActivity.this,"Image Uploaded Sucessfully!!",Toast.LENGTH_LONG).show();
       }
       else if(serverResponseMessage.equalsIgnoreCase("error"))
       {
           Toast.makeText(UploadFromGalleryActivity.this,"Cannot upload.Please try again",Toast.LENGTH_LONG).show();

       }
    }
}

NOTE : in string path2 i'm getting path like /mnt/sdcard/zoo.jpg...is that causing any problem?

my server side php file 开发者_如何学Cis

<?php
$uploaddir = 'CatchOfTheDayImages/';

$file = basename($_FILES['userfile']['name']);

$uploadfile = $uploaddir . $file;

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "OK";
}
 else {
echo "ERROR";
}
exit();
?>


This article might help to reconstruct your implementation

http://blog.sptechnolab.com/2011/03/09/android/android-upload-image-to-server/

Happy Coding!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜