Android - HttpClient上传图片失败

时间:2012-09-02 13:15:30

标签: android httpclient

使用Android 4.0.3将图像上传到服务器时出现问题

 @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
     super.onActivityResult(requestCode, resultCode, data);
     if (requestCode == 1 && resultCode == Activity.RESULT_OK)
     {
         Uri selectedImageUri = data.getData(); 
         String selectedImagePath = getPath(selectedImageUri); 

         Bitmap bitmap = BitmapFactory.decodeFile(selectedImagePath);           
         ByteArrayOutputStream stream = new ByteArrayOutputStream();
         Bitmap resizedDishImage = Bitmap.createScaledBitmap(bitmap, 150, 150, false);
         resizedDishImage.compress(Bitmap.CompressFormat.PNG, 90, stream); //compress to which format you want.
            byte [] byte_arr = stream.toByteArray();
            String image_str = Base64.encodeBytes(byte_arr);
            ArrayList<NameValuePair> nameValuePairs = new  ArrayList<NameValuePair>();

            nameValuePairs.add(new BasicNameValuePair("image",image_str));

            try{
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(ResearchConstants.UPDATE_IMAGE);
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse response = httpclient.execute(httppost);
                String the_string_response = convertResponseToString(response);
                Log.d("Response", the_string_response);
            }catch(Exception e){
                  System.out.println("Error in http connection "+e.toString());
            }

     }
 }

 public String convertResponseToString(HttpResponse response) throws IllegalStateException, IOException{

     String res = "";
     StringBuffer buffer = new StringBuffer();
     inputStream = response.getEntity().getContent();
     int contentLength = (int) response.getEntity().getContentLength();
     if (contentLength < 0){

     }
     else{
            byte[] data = new byte[512];
            int len = 0;
            try
            {
                while (-1 != (len = inputStream.read(data)) )
                {
                    buffer.append(new String(data, 0, len)); //converting to string and appending  to stringbuffer…..
                }
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            try
            {
                inputStream.close(); // closing the stream…..
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            res = buffer.toString();     // converting stringbuffer to string…..
            //System.out.println("Response => " +  EntityUtils.toString(response.getEntity()));
     }
     return res;
}

这是我的java代码。

$base=$_REQUEST['image'];
$binary=base64_decode($base);
header('Content-Type: bitmap;charset-utf-8');
$file = fopen('uploaded_image.png', 'wb');
fwrite($file, $binary);
fclose($file);
echo "Image Upload Complete!!, Check PHP File Directory.";

上面的代码是我的PHP。

我的结果是说上传的图片,但是当我检查文件目录时。没有这样的图像目录?任何人都可以要求解决方案或替代方案吗?

1 个答案:

答案 0 :(得分:0)

您正在主线程上执行HTTP操作。这是不正确的,会导致异常。但是,你会默默地捕获该异常 - 在日志中简单打印error in http connection。 在不同的线程上进行http操作。