将二进制流转换为字节?

时间:2013-03-18 05:21:34

标签: android file byte binary-data downloadfile

我有二进制流,我希望它以字节为单位进行转换。之后我想把它下载到SD卡中。

我的网络服务:

    POST /**********Mobile/**********.asmx HTTP/1.1
    Host: ***.***.*.*
    Content-Type: text/xml; charset=utf-8
    Content-Length: length
    SOAPAction: "http://tempuri.org/GetDocument"

    <?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">

    <soap:Body>
        <GetDocument xmlns="http://tempuri.org/">
           <DocumentID>string</DocumentID>
       </GetDocument>
    </soap:Body>
 </soap:Envelope>
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      
xmlns:xsd="http://www.w3.org/2001/XMLSchema"     
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
  <GetDocumentResponse xmlns="http://tempuri.org/">
    <GetDocumentResult>string</GetDocumentResult>
  </GetDocumentResponse>
</soap:Body>
</soap:Envelope>

我正在尝试的是

 SoapObject DocumentRequest = new SoapObject(NAMESPACE, GET_DOCUMENT_METHOD);

        DocumentRequest.addProperty("DocumentID", ID);
        Log.i("DocumentID", ID);

        SoapSerializationEnvelope Envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        Envelope.dotNet = true;
        Envelope.setOutputSoapObject(DocumentRequest);
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        androidHttpTransport.debug = true;

        androidHttpTransport.call(SOAP_ACTION_GETDOCUMENT, Envelope);

        SoapPrimitive DocumentResponse = (SoapPrimitive)Envelope.getResponse();
        Log.i("DocumentResponse", DocumentResponse.toString());

        String DocAsString = DocumentResponse.toString();

        byte[] decodedString = DocAsString.getBytes();

        Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
        ImageView DisplayDoc= (ImageView)findViewById(R.id.displayimage);
        DisplayDoc.setImageBitmap(decodedByte);
}

我正在获取二进制流作为字符串是正确的还是我需要使用base64binary。如果是,那么请让我知道下载image / .pdf / word doc的正确方法。我经历过差异搜索,但没有用。感谢

编辑:

SoapPrimitive DocumentResponse = (SoapPrimitive)Envelope.getResponse();
        Log.i("DocumentResponse", DocumentResponse.toString());

        String DocAsString = DocumentResponse.toString();

        byte[] decodedString = Base64.decode(DocAsString, Base64.DEFAULT);

        /*Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
        ImageView DisplayDoc= (ImageView)findViewById(R.id.displayimage);
        DisplayDoc.setImageBitmap(decodedByte);*/

         File direct = new File(Environment.getExternalStorageDirectory() + "/Downloads");

         if(!direct.exists())  {
             direct.mkdir(); }
        File photo=new File(Environment.getExternalStorageDirectory() + "/Downloads", ID);

         if (photo.exists()) {
              photo.delete(); }

     try {
       FileOutputStream fos=new FileOutputStream(photo.getPath());

       fos.write(decodedString);
       fos.close();

       ContentValues values= new ContentValues();

       System.out.println(values);

     }
     catch (java.io.IOException e) 
     {
       Log.e("PictureDemo", "Exception in photoCallback", e);
     }
    }
    catch(Exception e) {
        e.printStackTrace();
        Toast.makeText(getApplicationContext(), " Network Exception : " + e
                + "Please check network connectivity.", Toast.LENGTH_LONG).show();
    }
}

已编辑,网络服务响应

03-19 10:58:57.797: D/dalvikvm(789): GC_FOR_ALLOC freed 57K, 4% free 6912K/7171K, paused 53ms

03-19 10:58:57.807:I / dalvikvm-heap(789):将堆(frag case)增长到7.313MB,用于488976字节分配 03-19 10:58:57.957:​​D / dalvikvm(789):GC_CONCURRENT释放1K,4%释放7388K / 7687K,暂停4ms + 14ms 03-19 10:58:58.357:D / gralloc_goldfish(789):未检测到GPU仿真的仿真器。 03-19 10:58:58.497:W / TextLayoutCache(789):computeValuesWithHarfbuzz - 需要强制单次运行 03-19 10:59:02.327:D / InputEventConsistencyVerifier(789):KeyEvent:ACTION_UP但键未关闭。 03-19 10:59:02.327:D / InputEventConsistencyVerifier(789):在android.widget.EditText@41054cd8中 03-19 10:59:02.327:D / InputEventConsistencyVerifier(789):0:发送于2335155000000,KeyEvent {action = ACTION_UP,keyCode = KEYCODE_TAB,scanCode = 15,metaState = 0,flags = 0x8,repeatCount = 0,eventTime = 2335155,downTime = 2335119,deviceId = 0,source = 0x101} 03-19 10:59:04.488:I / LoginDetail(789):用户名jarnail@sassypuma.comPassword admin 03-19 10:59:04.537:D / dalvikvm(789):GC_FOR_ALLOC释放71K,3%免费7634K / 7815K,暂停40ms 03-19 10:59:04.937:I / myLogin(789):1 03-19 10:59:05.357:D / dalvikvm(789):GC_CONCURRENT释放360K,7%免费7698K / 8199K,暂停7ms + 23ms 03-19 10:59:05.547:I / AllFolders(789):anyType {TreeData = anyType {ID = 13002; FolderName =我的云端硬盘; PARENTID = 0; CreatedBy = 1; }; TreeData = anyType的{ID = 13006;文件夹名称= Testing1; PARENTID = 0; CreatedBy = 1; }; TreeData = anyType的{ID = 13007;文件夹名称=是testing123; PARENTID = 0; CreatedBy = 1; }; } 03-19 10:59:05.547:I / System.out(789):3 03-19 10:59:06.057:D / dalvikvm(789):GC_CONCURRENT释放362K,7%免费7761K / 8263K,暂停31ms + 7ms 03-19 10:59:06.217:W / TextLayoutCache(789):computeValuesWithHarfbuzz - 需要强制单次运行 03-19 10:59:06.627:W / IInputConnectionWrapper(789):在非活动InputConnection上的finishComposingText 03-19 10:59:08.307:I / FolderID(789):13002 03-19 10:59:08.307:I / User_ID(789):1 03-19 10:59:08.367:D / dalvikvm(789):GC_FOR_ALLOC释放16K,5%免费7874K / 8263K,暂停45ms 03-19 10:59:08.377:I / dalvikvm-heap(789):将堆(frag case)增长到8.036MB以进行262160字节分配 03-19 10:59:08.417:D / dalvikvm(789):GC_FOR_ALLOC释放3K,6%免费8127K / 8583K,暂停44ms 03-19 10:59:08.917:I / SubFolders(789):anyType {FileFolderDetails = anyType {ID = 13003; Name = Doc文件; URL = anyType的{}; SubjectType =文件夹; }; FileFolderDetails = anyType的{ID = 13004;姓名=我的照片; URL = anyType的{}; SubjectType =文件夹; }; FileFolderDetails = anyType的{ID = 13005; NAME =测试; URL = anyType的{}; SubjectType =文件夹; }; FileFolderDetails = anyType的{ID = 13075; NAME =萨特南; URL = anyType的{}; SubjectType =文件夹; }; FileFolderDetails = anyType的{ID = 13080; NAME =赛尼; URL = anyType的{}; SubjectType =文件夹; }; FileFolderDetails = anyType的{ID = 13084; NAME =果冻; URL = anyType的{}; SubjectType =文件夹; }; FileFolderDetails = anyType的{ID = 13092; NAME = pushie; URL = anyType的{}; SubjectType =文件夹; }; FileFolderDetails = anyType的{ID = 13096; NAME = Jarnail_Testing; URL = anyType的{}; SubjectType =文件夹; }; FileFolderDetails = anyType的{ID = 13099; NAME = amarjeet; URL = anyType的{}; SubjectType =文件夹; }; FileFolderDetails = anyType的{ID = 13101;名称= Kaam; URL = anyType的{}; SubjectType =文件夹; }; FileFolderDetails = anyType的{ID = 13103; NAME = tajmehal; URL = anyType的{}; SubjectType =文件夹; }; FileFolderDetails = anyType的{ID = 13105; NAME =文本区域; URL = anyType的{}; SubjectType =文件夹; }; FileFolderDetails = anyType的{ID = 13107; NAME = TextArea1; URL = anyType的{}; SubjectType =文件夹; }; FileFolderDetails = anyType的{ID = 13109; NAME = TextArea2; URL = anyType的{}; SubjectType =文件夹; }; FileFolderDetails = anyType的{ID = 13115; NAME = TextArea3; URL = anyType的{}; SubjectType =文件夹; }; FileFolderDetails = anyType的{ID = 13121; NAME = TextArea5; URL = anyType的{}; SubjectType =文件夹; }; FileFolderDetails = anyType的{ID = 13124; Name = Check Char; URL = anyType的{}; SubjectType =文件夹; }; FileFolderDetails = anyType的{ID = 13128; NAME = amitestinggggg; URL = anyType的{}; SubjectType =文件夹; }; FileFolderDetails = anyType的{ID = 13132;名称= KKK; URL = anyType的{}; SubjectType =文件夹; }; FileFolderDetails = anyType的{ID = 13136; NAME = hhhhhhhhhhhhhhhhhh; URL = anyType的{}; SubjectType =文件夹; }; FileFolderDetails = anyType的{ID = 13140; NAME = ravitevffvvgfdcvgbgvf; URL = anyType的{}; SubjectType =文件夹; }; FileFolderDetails = anyType的{ID = 13144; NAME = DMS3.0_Title_Page; URL = anyType的{}; SubjectType =文件夹; }; FileFolderDetails = anyType的{ID = 20; NAME = office.jpg; URL = InterLogicsMobile /图像/ Tulips.jpg; SubjectType =文件; }; FileFolderDetails = anyType的{ID = 21; NAME = Capture.PNG; URL = InterLogicsMobile /图像/ Tulips.jpg; SubjectType =文件; }; FileFolderDetails = anyType的{ID = 22; NAME = groups.jpg; URL = InterLogicsMobile /图像/ Tulips.jpg; SubjectType =文件; }; FileFolderDetails = anyType的{ID = 23; NAME = Capture1.PNG; URL = InterLogicsMobile /图像/ Tulips.jpg; SubjectType =文件; }; FileFolderDetails = anyType的{ID = 99; NAME = Content4Work.pdf; URL = InterLogicsMobile /图像/ Tulips.jpg; SubjectType =文件; }; FileFolderDetails = anyType的{ID = 109; NAME = Rohit.docx; URL = InterLogicsMobile /图像/ Tulips.jpg; SubjectType =文件; }; FileFolderDetails = anyType的{ID = 115; NAME = Chrysanthemum.jpg; URL = InterLogicsMobile /图像/ Tulips.jpg; SubjectType =文件; }; FileFolderDetails = anyType的{ID = 123;名称= DMS_Drag_and_Drop(2).DOC; URL = InterLogicsMobile /图像/ Tulips.jpg; SubjectType =文件; }; FileFolderDetails = anyType的{ID = 124; NAME = Tulips.jpg; URL = InterLogicsMobile /图像/ Tulips.jpg; SubjectType =文件; }; FileFolderDetails = anyType的{ID = 125; NAME = Jellyfish.jpg; URL = InterLogicsMobile /图像/ Tulips.jpg; SubjectType =文件; }; FileFolderDetails = anyType的{ID = 126; NAME = Koala.jpg; URL = InterLogicsMobile /图像/ Tulips.jpg; SubjectType =文件; }; FileFolderDetails = anyType的{ID = 127; NAME = Lighthouse.jpg; URL = InterLogicsMobile /图像/ Tulips.jpg; SubjectType =文件; }; FileFolderDetails = anyType的{ID = 128; NAME = Jellyfish.jpg; URL = InterLogicsMobile /图像/ Tulips.jpg; SubjectType =文件; }; FileFolderDetails = anyType的{ID = 129; NAME = Jellyfish.jpg; URL = InterLogicsMobile /图像/ Tulips.jpg; SubjectType =文件; }; FileFolderDetails = anyType的{ID = 130; NAME = Jellyfish.jpg; URL = InterLogicsMobile /图像/ Tulips.jpg; SubjectType =文件; }; FileFolderDetails = anyType的{ID = 131; NAME = Jellyfish.jpg; URL = HTTP 03-19 10:59:08.917:I / System.out(789):41 03-19 10:59:09.527:D / dalvikvm(789):GC_CONCURRENT释放571K,9%免费8012K / 8711K,暂停23ms + 26ms 03-19 10:59:12.787:D / dalvikvm(789):GC_FOR_ALLOC释放11K,5%免费8352K / 8711K,暂停49ms 03-19 10:59:12.857:D / dalvikvm(789):GC_FOR_ALLOC释放1K,3%释放8712K / 8967K,暂停50ms 03-19 10:59:12.937:D / dalvikvm(789):GC_FOR_ALLOC释放&lt; 1K,4%free 9163K / 9479K,暂停48ms 03-19 10:59:15.427:I / System.out(789):13003 03-19 10:59:15.427:I / DocumentID(789):13003 03-19 10:59:15.837:D / dalvikvm(789):GC_CONCURRENT释放328K,5%自由9581K / 10055K,暂停23ms + 5ms 十月3日至19日:59:15.867:I / DocumentResponse(789):UEsDBBQABgAIAAAAIQAJJIeCgQEAAI4FAAATAAgCW0NvbnRlbnRfVHlwZXNdLnhtbCCiBAIooAACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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cQVofqvsdCtKE74rSxndQa8x1ATBhykMAVV + zC6okGIRUNpcymIDWfNvlL1hy1 / u5CvqcFM4xJKsQdswZ1IRloZcVblwdV3X9WXiP3oUjB1RRzEjsM1FI59Nkw4pmIH0y01h8kFRbynvxdartWOjqLvQ3sQqx2WE + YbmEUjr7zD0MybAFrSfSqwIgHiSfyQC6nxkgGjsj9EriODKQyLpUy37l + hMoZhk / 6YBFE0H4zGw3nQLM1IhtfM + ifjUXQ795bQSjKbaw277FYBrlGEsSeLtQ1CB6ndj53OuLkL3YX79WunLeciPbRz4yY2CieArDQxRG9IMEVXQbVJwNxbXWV6HQsYdUjTvLCo6yeRaoswY0ekIEGqXYwq5ZeLUeX6dM6aaYhMIUtznMHuHltFzDXm7cT / KohAqCRHcUKYL5vHWV + Xezfo0MMzogIJUqJUloJJnJL 0TCma4 / C4C7sHvpRy5Y43HytUhaYgDKcVgTn06u + F / IKTVdXKzV6IbLcTtNHNuRfVsU5fa40OJr7 / XlbnnLSz1H9fK643r6v7P8feM

1 个答案:

答案 0 :(得分:5)

                    try {
                    ht.call(SOAP_ACTION, envelope);

                    final SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
                    String str =DocumentResponse.toString();

                        byte[] decodedString = Base64.decode(str, Base64.DEFAULT);

                   File direct = new File(Environment.getExternalStorageDirectory() + "/Foldername");

                      if(!direct.exists())
                      {
                          direct.mkdir();
                      }

                    File photo=new File(Environment.getExternalStorageDirectory() + "/Foldername",Filename);


                  if (photo.exists())
                  {
                        photo.delete();
                  }

                  try {
                    FileOutputStream fos=new FileOutputStream(photo.getPath());

                    fos.write(decodedString);
                    fos.close();

                    ContentValues values= new ContentValues();

                  }
                  catch (java.io.IOException e) 
                  {
                    Log.e("PictureDemo", "Exception in photoCallback", e);
                  }
                }


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

将您的String转换为Base64解码。

示例:byte [] decodingString = Base64.decode(str,Base64.DEFAULT);

使用该字节将文件保存在SD卡中。

例:fos.write(decodedString);