2017-04-19 56 views
0

我正在開發一個android應用程序在我的應用程序中我想將圖像的字節數組轉換爲base64字符串。但是當我轉換時,我得到了損壞的結果。獲取損壞的編碼數據,同時將字節數組轉換爲base64字符串android

這是我的AsyncTask

public class Test1 extends AsyncTask<Void, byte[], String> { 
    @Override 
    protected String doInBackground(Void... voids) { 

     try { 
      myClient2 = new Socket("192.168.1.1", 6666); 

      System.out.println("a:" + myClient2); 
      try { 
       JSONObject jsonObject1 = new JSONObject(); 
       jsonObject1.put("cmd", "request_download_file"); 
       jsonObject1.put("file_name", filename1); 
       jsonObject1.put("verification", "CVS"); 


       System.out.println("File name========" + filename1); 

       System.out.println("b111111111111111" + jsonObject1); 
       myClient2.setKeepAlive(true); 
       System.out.println(myClient2.getLocalAddress() + " " + myClient2.getLocalSocketAddress() + " " + myClient2.getKeepAlive()); 
       System.out.println(myClient2.getKeepAlive()); 
       try { 

        DataInputStream dis1 = new DataInputStream(myClient2.getInputStream()); 

        //reads file length from datainputstream 
        DataOutputStream dos1 = new DataOutputStream(myClient2.getOutputStream()); 
        jsonReader1 = new JsonReader(new InputStreamReader(dis1));//, "UTF-8")); 

        InputStreamReader inputReader = new InputStreamReader(dis1); 

        writer1 = new OutputStreamWriter(dos1);//, StandardCharsets.UTF_8) 
        writer1.write(jsonObject1.toString()); 
        System.out.println("c"); 
        writer1.flush(); 

        BufferedInputStream input = new BufferedInputStream(myClient2.getInputStream()); 
        Log.e("input", "input" + input); 

        byte[] data = new byte[1024]; 

        BufferedReader brdr = new BufferedReader(inputReader, 300); 

        boolean status = false; 


        int countPackets = 0; 

        while (!status) { 

         byte[] buffer = new byte[1024]; // or 4096, or more 

         int bytesRead = input.read(buffer, 0, buffer.length); 

         int availableBytes = input.available(); 


         str2 = str2.append(bytesRead); 

         ImageDataArray = concat(ImageDataArray, buffer); 

         countPackets = countPackets + buffer.length; 

         System.out.println(ImageDataArray.length); 




         System.out.println(countPackets); 

         System.out.println("get" + ImageDataArray); 


         String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss") 
           .format(new Date()); 


         System.out.println("check--" + timeStamp + "--"); 


         if (input.available() != 0) { 
          System.out.println("String:" + str1.toString()); 
         } else break; 
        } 

        System.out.println("--------------------out of while ------------------"); 

        System.out.println("filename===========" + filename1); 
        // String filename = String.format("%s%s", Environment.getExternalStorageDirectory().getAbsoluteFile(), "/SteelmanPro/1020/images/image34" + ".jpg"); 

        File file = new File(Environment.getExternalStorageDirectory().getAbsoluteFile().toString(), "/SteelmanPro/1020/images/image"); 



        if (!file.exists()) { 

         file.mkdirs(); 
         System.out.println("File Created++++++++++++++++++"); 

        } 


        int position = - 1; 

        for (int i = 0; i < ImageDataArray.length - 2; i++) { 
         if (ImageDataArray[i] == (byte) 0xff && ImageDataArray[i + 1] == (byte) 0xdf) { 
          position = i; 
          Log.e("position", "position." + position); 

          break; 
         } 
        } 


        write(ImageDataArray,file.toString() +filename1); 


        String encodedImage = Base64.encodeToString((byte[])ImageDataArray, Base64.DEFAULT); 
        Log.i("Base64=========",encodedImage.toString()); 


        int a = data.length; 

        Log.e("data.length", "data.length" + a); 



        System.out.println("String:" + str1.toString()); 
        normalLoginResponse = hexChars; 


       } catch (Exception e) { 
        e.printStackTrace(); 
       } 

      } catch (Exception e) { 
       e.printStackTrace(); 
       System.out.println("send JSON error"); 
      } 
      System.out.println("d"); 
      System.out.println("e"); 
      System.out.println("f"); 
      System.out.println("g"); 

     } catch (Exception e) { 
      e.printStackTrace(); 
      System.out.println("creation exception1"); 

     } 

     return normalLoginResponse; 
    } 


    @Override 
    protected void onPostExecute(String response) { 

} 

logcat的

Base64=========: eyJjbWQiOiJyZXNwb25zZV9kb3dubG9hZF9maWxlIiwicmVzdWx0Ijoic3VjY2VzcyIsImZpbGVf 
                  c2l6ZSI6IjQwMzgzMyJ9DQoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 
                  AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 
                  AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 
                  AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 
                  AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 
                  AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 
                  AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 
                  AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 
                  AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 
                  AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 
                  AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 
                  AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 
                  AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 
                  AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 
                  AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 
                  AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 
                  AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP/Y 
                  /+ERHUV4aWYAAE1NACoAAAAIAAwBAAADAAAAAQQAAAABAQADAAAAAQMAAAABAgADAAAAAwAAAJ4B 
                  BgADAAAAAQACAAABEgADAAAAAQABAAABFQADAAAAAQADAAABGgAFAAAAAQAAAKQBGwAFAAAAAQAA 
                  AKwBKAADAAAAAQACAAABMQACAAAAHAAAALQBMgACAAAAFAAAANCHaQAEAAAAAQAAAOQAAAEcAAgA 
                  CAAIAAr8gAAAJxAACvyAAAAnEEFkb2JlIFBob3Rvc2hvcCBDUzUgV2luZG93cwAyMDE2OjA1OjI1 
                  IDE4OjE4OjM2AAAEkAAABwAAAAQwMjIxoAEAAwAAAAH//wAAoAIABAAAAAEAAAXcoAMABAAAAAEA 
                  AARlAAAAAAAAAAYBAwADAAAAAQAGAAABGgAFAAAAAQAAAWoBGwAFAAAAAQAAAXIBKAADAAAAAQAC 
                  AAACAQAEAAAAAQAAAXoCAgAEAAAAAQAAD5sAAAAAAAAASAAAAAEAAABIAAAAAf/Y/+0ADEFkb2Jl 
                  X0NNAAL/7gAOQWRvYmUAZIAAAAAB/9sAhAAMCAgICQgMCQkMEQsKCxEVDwwMDxUYExMVExMYEQwM 
                  DAwMDBEMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMAQ0LCw0ODRAODhAUDg4OFBQODg4OFBEM 
                  DAwMDBERDAwMDAwMEQwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAz/wAARCAB4AKADASIAAhEB 
                  AxEB/90ABAAK/8QBPwAAAQUBAQEBAQEAAAAAAAAAAwABAgQFBgcICQoLAQABBQEBAQEBAQAAAAAA 
                  AAABAAIDBAUGBwgJCgsQAAEEAQMCBAIFBwYIBQMMMwEAAhEDBCESMQVBUWETInGBMgYUkaGxQiMk 
                  FVLBYjM0coLRQwclklPw4fFjczUWorKDJkSTVGRFwqN0NhfSVeJl8rOEw9N14/NGJ5SkhbSVxNTk 
                  9KW1xdXl9VZmdoaWprbG1ub2N0dXZ3eHl6e3x9fn9xEAAgIBAgQEAwQFBgcHBgU1AQACEQMhMRIE 
                  QVFhcSITBTKBkRShsUIjwVLR8DMkYuFygpJDUxVjczTxJQYWorKDByY1wtJEk1SjF2RFVTZ0ZeLy 
                  s4TD03Xj80aUpIW0lcTU5PSltcXV5fVWZnaGlqa2xtbm9ic3R1dnd4eXp7fH/9oADAMBAAIRAxEA 
                  PwDgk8lKE8IKVuKeSmhOAkpcEqQKiApAJKXkp5KQCeElKkpSU4anDUlLap5Kfan2pKWkp5KfaltS 
                  UtuKUlPtT7UlMEjKntTbUlIyE0Im1NtSU//Q4WE8KQCUIKYwpAJwFIBJTENUg1SAUg1JTENUg1SD 
                  VMNSUwDU4aiBqkGpKRhifYihik2pzvotLvgJSUh2JbFYOPY36Tdv9Ytb/wBW5qhDCYD2EjmHsP5H 
                  I0pFtS2ooDXGGlrj4Ag/9SU5rI5BQUg2pbUbamLUlIdqYtRS1MWpKf/R4kJ0wCkgpQCkAmAKkAUl 
                  MgFIBMNFJ1dwg2OFFEAuI/nXa/RE/QYipk1smAJJ7DlTAG4s1e8c1sgkf13H9HX/AGnKo7qFMEe/ 
                  Yf8AB1+0n/jr3f8AUVKDuqWhuylrKGDhrBP/AEne3/oJKbd1jqiXlllVQ72sDwD/AMbj2H/z0gDK 
                  e+0WU5WM5kQKLCWj+t+l9F27+0qF2QbtLi+wDUAkxPkPooYHtJLRtJ0JS+inUbj9Sex5LfWfINPp 
                  uDmdw9rxU5/t+jYr+E7Ma1wdVlW2NYC4VU7pLfZvt/Oa1v8AIrsXNhrR7toA117Lr8DH/wCbHQB1 
                  7Ol/VM4FvSMWwktqaRrn21O/wux36H/RIhDiVW4VLTYWVX7eA6Zn930v3lpUZzrW1hgZQyzRrQAD 
                  v4Nbmw1u9jvzFz2U8jIeQ42PLjve8kuJIHuc785yYZB+y2Vk7fex7YPxrf8A+iv8xJLvO6X1fLv/ 
                  AEtWFdWD7LLvYf8AwL6P9tqpWZzMK44uZi24VrPpMaQ9on84bfT3Md/pK/UWl0rqjsjFY60EXCGP 
                  EH3GPbY3+u36av5XQczrLGV3Y/pMrMsvtOxwB+kxjYfa5j/3PTQA7Ktxce+rIe0UZDXBxAJJ+iJ1 
                  e9j9r2tYim0N0llx8WtLRH9f2+5bLPq30HpbYy8gNd3Y2GE/+fst/wDnJzk/VatjmNx9zSCC7Y4u 
                  /s22u3sd/KRrxVY7OJuYeJHkUiFXry6bMxuO1r2kn2F4gOj3R/m/yUcnUoEKf//S44VlTFSstoKM 
                  zGsdu9Ot1prY6xzWDcQxn85Y79ytn59j/Ykpp+mGgudo0ckqDb6QfoFw+MIOS592TW6u0WVtjdWG 
                  uaGbmhz5dZHqWMe70fb9P0/Ur/QojWHsISGotR00bD8prqHspa2i0j2PAEgj+UdzljZH24kuvD3/ 
                  AMrkfe1araUZlUI0i3n2WsJh2h80YMrdEHnhbjsSi0fpa2v/AKwBP3qI6TgkAeloOBudA+HuSpTj 
                  CkTEx4k/3InpVBoDtdvGq2WdJwhxWR/bd/5JWsL6sDqLzXjUPsA+m42ObW3/AIywu2f2fppUprfU 
                  /oWL1DOszc0x0jpjfWzHWaMe8TZVj/yqtrfWv/4P9H/hU/WbOs/Wfq 

如果有人知道這個請大家幫我

+1

ImageDataArray - 它是你的字節[]嗎? –

+0

那麼什麼是損壞的? – greenapps

+0

你應該告訴服務器正在發送什麼。你從插座讀取什麼。您是否將發送的字節數與接收到的字節數進行了比較?數字請。 – greenapps

回答

0

您正在使用的BufferedReader接收圖像文件的字節。

這是行不通的,因爲您已經看到,作爲讀者的文本數據。

因此,請放棄它。

相關問題