久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

  • <tfoot id='6lLcq'></tfoot>

      <small id='6lLcq'></small><noframes id='6lLcq'>

      • <bdo id='6lLcq'></bdo><ul id='6lLcq'></ul>

      <i id='6lLcq'><tr id='6lLcq'><dt id='6lLcq'><q id='6lLcq'><span id='6lLcq'><b id='6lLcq'><form id='6lLcq'><ins id='6lLcq'></ins><ul id='6lLcq'></ul><sub id='6lLcq'></sub></form><legend id='6lLcq'></legend><bdo id='6lLcq'><pre id='6lLcq'><center id='6lLcq'></center></pre></bdo></b><th id='6lLcq'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='6lLcq'><tfoot id='6lLcq'></tfoot><dl id='6lLcq'><fieldset id='6lLcq'></fieldset></dl></div>

    1. <legend id='6lLcq'><style id='6lLcq'><dir id='6lLcq'><q id='6lLcq'></q></dir></style></legend>
      1. 將圖像從android上傳到PHP服務(wù)器

        Uploading Image from android to PHP server(將圖像從android上傳到PHP服務(wù)器)
        • <small id='siHBk'></small><noframes id='siHBk'>

        • <tfoot id='siHBk'></tfoot><legend id='siHBk'><style id='siHBk'><dir id='siHBk'><q id='siHBk'></q></dir></style></legend>

          <i id='siHBk'><tr id='siHBk'><dt id='siHBk'><q id='siHBk'><span id='siHBk'><b id='siHBk'><form id='siHBk'><ins id='siHBk'></ins><ul id='siHBk'></ul><sub id='siHBk'></sub></form><legend id='siHBk'></legend><bdo id='siHBk'><pre id='siHBk'><center id='siHBk'></center></pre></bdo></b><th id='siHBk'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='siHBk'><tfoot id='siHBk'></tfoot><dl id='siHBk'><fieldset id='siHBk'></fieldset></dl></div>
            <bdo id='siHBk'></bdo><ul id='siHBk'></ul>

                    <tbody id='siHBk'></tbody>
                  本文介紹了將圖像從android上傳到PHP服務(wù)器的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  限時送ChatGPT賬號..

                  在我的應(yīng)用程序中,我正在將圖像從我的設(shè)備上傳到本地網(wǎng)絡(luò)服務(wù)器...執(zhí)行代碼后,會在服務(wù)器中創(chuàng)建一個 .jpg 文件,但不會打開它.并且服務(wù)器中文件的大小與原始文件不同.

                  In my app i am uploading an image from my device to a local web server... after executing the code a .jpg file gets created in the server but it does not gets opened. And the size of the file in server is different from the original file.

                  Android 活動:--

                  public class MainActivity extends Activity {
                  
                  
                  private static int RESULT_LOAD_IMAGE = 1;
                  @Override
                  protected void onCreate(Bundle savedInstanceState) {
                      super.onCreate(savedInstanceState);
                      setContentView(R.layout.activity_main);
                  
                      Button btnSelectImage=(Button) findViewById(R.id.uploadButton);
                      btnSelectImage.setOnClickListener(new OnClickListener() {
                  
                          @Override
                          public void onClick(View v) {
                  
                          Intent i=new   Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                  
                          startActivityForResult(i, RESULT_LOAD_IMAGE);
                  
                          }
                      });
                  
                  }
                  
                  @Override
                  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                      // TODO Auto-generated method stub
                      super.onActivityResult(requestCode, resultCode, data);
                  
                      if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && data!=null) {
                  
                          Uri selectedImage=data.getData();
                          String[] filePathColumn={MediaStore.Images.Media.DATA};
                  
                          Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
                          cursor.moveToFirst();
                  
                          int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                          String picturePath = cursor.getString(columnIndex);
                          cursor.close();
                  
                  
                          Bitmap bitmap=BitmapFactory.decodeFile(picturePath);
                  
                          ImageView im = (ImageView) findViewById(R.id.imgBox);
                          im.setImageBitmap(bitmap);
                  
                          /*
                           * Convert the image to a string
                           * */
                          ByteArrayOutputStream stream = new ByteArrayOutputStream();
                          bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream); //compress to which format you want.
                          byte [] byte_arr = stream.toByteArray();
                          String image_str = Base64.encodeToString(byte_arr,Base64.DEFAULT);
                  
                          /*
                           * Create a name value pair for the image string to be passed to the server
                           * */
                          ArrayList<NameValuePair> nameValuePairs = new  ArrayList<NameValuePair>();            
                          nameValuePairs.add(new BasicNameValuePair("image",image_str));
                  
                  
                          JSONObject jsonString=new JSONObject();
                          try {
                              jsonString.put("img", image_str);
                          } catch (JSONException e) {
                              // TODO Auto-generated catch block
                              e.printStackTrace();
                          }
                  
                          new uploadImageToPhp().execute(jsonString);
                  
                  
                  
                      }
                  
                  
                  }
                  @Override
                  public boolean onCreateOptionsMenu(Menu menu) {
                      // Inflate the menu; this adds items to the action bar if it is present.
                      getMenuInflater().inflate(R.menu.main, menu);
                  
                  
                      return true;
                  }
                  public class uploadImageToPhp extends AsyncTask<JSONObject, Void, Void>
                  {
                      String dataToSend=null;
                  
                      public static final String prefix="http://";                                                        //prefix of the urls
                      public static final String server_ip="172.16.26.155";                                                   //the ip address where the php server is located    
                  
                      public static final String completeServerAddress=prefix+server_ip+"/test_upload/upload_image.php";                  //Exact location of the php files
                  
                      @Override
                      protected Void doInBackground(JSONObject... params) {
                  
                          dataToSend="image="+params[0];
                          communicator(completeServerAddress, dataToSend);
                  
                  
                  
                  
                          return null;
                      }
                  
                      public void communicator(String urlString,String dataToSend2)
                      {
                          String result=null;
                  
                          try
                          {
                              URL url=new URL(urlString);
                              URLConnection conn=url.openConnection();
                  
                              HttpURLConnection httpConn=(HttpURLConnection) conn;
                              httpConn.setRequestProperty("Accept", "application/json");
                              httpConn.setRequestProperty("accept-charset", "UTF-8");
                              httpConn.setRequestMethod("POST");         
                              httpConn.connect();
                  
                              //Create an output stream to send data to the server
                              OutputStreamWriter out=new OutputStreamWriter(httpConn.getOutputStream());
                              out.write(dataToSend2);
                              out.flush();
                  
                              int httpStatus = httpConn.getResponseCode();            
                              System.out.println("Http status :"+httpStatus);
                  
                              if(httpStatus==HttpURLConnection.HTTP_OK)
                              {
                                  Log.d("HTTP STatus", "http connection successful");
                  
                                  BufferedReader in = new BufferedReader(new InputStreamReader(httpConn.getInputStream(),"UTF-8"));
                                  StringBuilder sb = new StringBuilder();
                                  String inputLine;
                                  while ((inputLine = in.readLine()) != null)
                                  {
                                      System.out.println(inputLine);
                                      sb.append(inputLine+"
                  ");
                                  }
                                  in.close();
                                  result=sb.toString();                       
                  
                                  try
                                  {
                  
                                      //jsonResult = new JSONObject(result);
                                  }
                                  catch(Exception e)
                                  {
                                       Log.e("JSON Parser", "Error parsing data " + e.toString());
                                  }
                  
                  
                              }
                              else
                              {
                                  System.out.println("Somthing went wrong");
                              }
                          } catch (MalformedURLException e) {
                              // TODO Auto-generated catch block
                              e.printStackTrace();
                          } catch (IOException e) {
                              // TODO Auto-generated catch block
                              e.printStackTrace();
                          }
                  
                  
                      }
                  
                  
                  }
                  
                  }
                  

                  PHP 代碼:-

                   $recievedJson=$_REQUEST['image'];
                  $imageContent=json_decode($recievedJson,true);
                   $base=$imageContent["img"];
                  
                   $binary=base64_decode($base);
                  
                   echo $binary;
                  header('Content-Type: bitmap; charset=utf-8');
                  $file = fopen('uploaded_image.jpg', 'wb');
                  fwrite($file, $binary);
                  fclose($file);
                  

                  推薦答案

                  使用下面的代碼.它會做同樣的事情.

                  Use below code. It will do the same.

                  public class UploadImage extends Activity {
                      InputStream inputStream;
                          @Override
                      public void onCreate(Bundle icicle) {
                              super.onCreate(icicle);
                              setContentView(R.layout.main);
                  
                              Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.icon);           ByteArrayOutputStream stream = new ByteArrayOutputStream();
                              bitmap.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));
                  
                               Thread t = new Thread(new Runnable() {
                  
                              @Override
                              public void run() {
                                    try{
                                           HttpClient httpclient = new DefaultHttpClient();
                                           HttpPost httppost = new HttpPost("server-link/folder-name/upload_image.php");
                                           httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                                           HttpResponse response = httpclient.execute(httppost);
                                           String the_string_response = convertResponseToString(response);
                                           runOnUiThread(new Runnable() {
                  
                                                  @Override
                                                  public void run() {
                                                      Toast.makeText(UploadImage.this, "Response " + the_string_response, Toast.LENGTH_LONG).show();                          
                                                  }
                                              });
                  
                                       }catch(Exception e){
                                            runOnUiThread(new Runnable() {
                  
                                              @Override
                                              public void run() {
                                                  Toast.makeText(UploadImage.this, "ERROR " + e.getMessage(), Toast.LENGTH_LONG).show();                              
                                              }
                                          });
                                             System.out.println("Error in http connection "+e.toString());
                                       }  
                              }
                          });
                           t.start();
                          }
                  
                          public String convertResponseToString(HttpResponse response) throws IllegalStateException, IOException{
                  
                               String res = "";
                               StringBuffer buffer = new StringBuffer();
                               inputStream = response.getEntity().getContent();
                               int contentLength = (int) response.getEntity().getContentLength(); //getting content length…..
                                runOnUiThread(new Runnable() {
                  
                              @Override
                              public void run() {
                                  Toast.makeText(UploadImage.this, "contentLength : " + contentLength, Toast.LENGTH_LONG).show();                     
                              }
                          });
                  
                               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…..
                  
                                      runOnUiThread(new Runnable() {
                  
                                      @Override
                                      public void run() {
                                         Toast.makeText(UploadImage.this, "Result : " + res, Toast.LENGTH_LONG).show();
                                      }
                                  });
                                      //System.out.println("Response => " +  EntityUtils.toString(response.getEntity()));
                               }
                               return res;
                          }
                  }
                  

                  PHP 代碼

                  <?php
                      $base=$_REQUEST['image'];
                       $binary=base64_decode($base);
                      header('Content-Type: bitmap; charset=utf-8');
                      $file = fopen('uploaded_image.jpg', 'wb');
                      fwrite($file, $binary);
                      fclose($file);
                      echo 'Image upload complete!!, Please check your php file directory……';
                  ?>
                  

                  更新

                  NameValuePair 和 Http 類已被棄用,所以我已經(jīng)嘗試過這段代碼,它對我有用.希望有幫助!

                  NameValuePair and Http Classes are deprecated so, I've tried this code and it's working for me. Hope that helps!

                  private void uploadImage(Bitmap imageBitmap){
                      ByteArrayOutputStream stream = new ByteArrayOutputStream();
                      imageBitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);
                      byte[] b = stream.toByteArray();
                      String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
                      ArrayList<Pair<String, String>> params = new ArrayList<Pair<String, String>>();
                      params.add(new Pair<>("image", encodedImage));
                  
                      try {
                          new AsyncUploader().execute(my_upload_php, getQuery(params));
                      } catch (UnsupportedEncodingException e) {
                          e.printStackTrace();
                      }
                  }
                  
                  private String getQuery(List<Pair<String, String>> params) throws UnsupportedEncodingException{
                      StringBuilder result = new StringBuilder();
                      boolean first = true;
                  
                      for(Pair<String, String> pair : params){
                          if(first)
                              first = false;
                          else
                              result.append("&");
                  
                          result.append(URLEncoder.encode(pair.first, "UTF-8"));
                          result.append("=");
                          result.append(URLEncoder.encode(pair.second, "UTF-8"));
                      }
                      return result.toString();
                  }
                  
                  private class AsyncUploader extends AsyncTask<String, Integer, String>
                  {
                      @Override
                      protected String doInBackground(String... strings) {
                          String urlString = strings[0];
                          String params = strings[1];
                          URL url = null;
                          InputStream stream = null;
                          HttpURLConnection urlConnection = null;
                          try {
                              url = new URL(urlString);
                              urlConnection = (HttpURLConnection) url.openConnection();
                              urlConnection.setRequestMethod("POST");
                              urlConnection.setDoOutput(true);
                  
                              urlConnection.connect();
                  
                              OutputStreamWriter wr = new OutputStreamWriter(urlConnection.getOutputStream());
                              wr.write(params);
                              wr.flush();
                  
                              stream = urlConnection.getInputStream();
                              BufferedReader reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"), 8);
                              String result = reader.readLine();
                              return result;
                          }catch (IOException ioe){
                              ioe.printStackTrace();
                          } finally {
                              if (urlConnection != null)
                                  urlConnection.disconnect();
                          }
                          return null;
                      }
                  
                      @Override
                      protected  void onPostExecute(String result) {
                          Toast.makeText(MakePhoto.this, result, Toast.LENGTH_SHORT).show();
                      }
                  }
                  

                  這篇關(guān)于將圖像從android上傳到PHP服務(wù)器的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

                  相關(guān)文檔推薦

                  enable SOAP on PHP(在 PHP 上啟用 SOAP)
                  Get received XML from PHP SOAP Server(從 PHP SOAP 服務(wù)器獲取接收到的 XML)
                  not a valid AllXsd value(不是有效的 AllXsd 值)
                  PHP SoapClient: SoapFault exception Could not connect to host(PHP SoapClient:SoapFault 異常無法連接到主機(jī))
                  Implementation of P_SHA1 algorithm in PHP(PHP中P_SHA1算法的實現(xiàn))
                  Sending a byte array from PHP to WCF(將字節(jié)數(shù)組從 PHP 發(fā)送到 WCF)
                    <tbody id='z5DJB'></tbody>
                      <bdo id='z5DJB'></bdo><ul id='z5DJB'></ul>
                      <legend id='z5DJB'><style id='z5DJB'><dir id='z5DJB'><q id='z5DJB'></q></dir></style></legend>
                    • <i id='z5DJB'><tr id='z5DJB'><dt id='z5DJB'><q id='z5DJB'><span id='z5DJB'><b id='z5DJB'><form id='z5DJB'><ins id='z5DJB'></ins><ul id='z5DJB'></ul><sub id='z5DJB'></sub></form><legend id='z5DJB'></legend><bdo id='z5DJB'><pre id='z5DJB'><center id='z5DJB'></center></pre></bdo></b><th id='z5DJB'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='z5DJB'><tfoot id='z5DJB'></tfoot><dl id='z5DJB'><fieldset id='z5DJB'></fieldset></dl></div>

                          <small id='z5DJB'></small><noframes id='z5DJB'>

                            <tfoot id='z5DJB'></tfoot>
                            主站蜘蛛池模板: 国产精品中文字幕一区二区三区 | 成人免费观看男女羞羞视频 | 国精产品一区二区三区 | 国产精品99一区二区 | 91国内精品久久 | 欧美在线免费 | 欧美一级三级 | 亚洲精品视频三区 | 久久久久www | 久久久久久久综合 | 国产精品久久久久久久久婷婷 | 日韩精品一区二区久久 | 成人免费看| 国产精品久久影院 | 日韩精品一区二区三区在线观看 | 欧美一区二区三区在线观看 | www.v888av.com| 婷婷在线视频 | 精品一区二区久久久久久久网站 | 日本一区二区三区在线观看 | 天堂亚洲网 | 日韩欧美大片在线观看 | 91在线播| 久久精品亚洲 | 伊人超碰在线 | 欧美成人精品欧美一级 | 亚洲成人中文字幕 | 久久国产精品久久国产精品 | 中文字幕av高清 | 亚洲视频免费播放 | www.亚洲一区 | 色综合一区二区三区 | 国产日韩欧美 | 免费在线视频精品 | 青春草国产 | 欧美综合一区二区三区 | 国产乱性 | 亚洲精品乱码 | 91精品国产一区二区三区 | 欧美一区二区三区四区视频 | 欧美性猛交 |