0

我在使用 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.ini 文件。

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

4

1 回答 1

0

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

于 2012-09-02T16:07:53.880 回答