1

바이트로 데이터를 다운로드하고 jpg 파일로 저장하면 쇼 이미지입니다. 할 수는 있지만 문제가 있습니다. I는 데이터를 다운로드하기 위해 코드를 사용하는 경우 이는 아무런 문제가 없지만,이 경우에는 다운로드 속도가 낮고 :데이터를 안드로이드에서 byte []로 다운로드하십시오.

String urlAddress = urlAddresses[0]; 
      try { 
       URL url = new URL(urlAddress); 
       HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection(); 
       httpURLConnection.setRequestMethod("GET"); 
       httpURLConnection.setDoInput(true); 
       httpURLConnection.connect(); 

       int fileLength = httpURLConnection.getContentLength(); 
       InputStream inputStream = httpURLConnection.getInputStream(); 
       byte[] buffer = new byte[1]; 
       int curLength = 0; 
       int newLength = 0; 
       ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 

       while((newLength = inputStream.read(buffer))>0) 
       { 
        curLength += newLength; 
        publishProgress(fileLength != 0 ? (curLength*100)/fileLength : -1); 
        byteArrayOutputStream.write(buffer); 
       } 

       if(fileLength > 0 && curLength != fileLength) 
       { 
        Byte[] result = new Byte[1]; 
        result[0] = ERROR_FILE_LENGTH; 
        return result; 
       } 
       complate = true; 
       byte[] resultByteArray = byteArrayOutputStream.toByteArray(); 
       Byte[] result = new Byte[resultByteArray.length]; 
       for(int i=0; i<result.length; i++) 
        result[i] = resultByteArray[i]; 
       return result; 

      } 
      catch (IOException e) 
      { 
       e.printStackTrace(); 
       Byte[] result = new Byte[1]; 
       result[0] = ERROR_DOWNLOAD_FAILED; 
       return result; 
      } 

하지만이 라인 변경하는 경우 : 바이트 [완충액 = 새로운 바이트 [1] 바이트 [] 버퍼 = 새로운 바이트 [1024] 다운로드 속도 올라가하지만 난 somename.jpg 파일에 저장할 때 이미지

String urlAddress = urlAddresses[0]; 
      try { 
       URL url = new URL(urlAddress); 
       HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection(); 
       httpURLConnection.setRequestMethod("GET"); 
       httpURLConnection.setDoInput(true); 
       httpURLConnection.connect(); 

       int fileLength = httpURLConnection.getContentLength(); 
       InputStream inputStream = httpURLConnection.getInputStream(); 
       byte[] buffer = new byte[1024]; 
       int curLength = 0; 
       int newLength = 0; 
       ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 

       while((newLength = inputStream.read(buffer))>0) 
       { 
        curLength += newLength; 
        publishProgress(fileLength != 0 ? (curLength*100)/fileLength : -1); 
        byteArrayOutputStream.write(buffer); 
       } 

       if(fileLength > 0 && curLength != fileLength) 
       { 
        Byte[] result = new Byte[1]; 
        result[0] = ERROR_FILE_LENGTH; 
        return result; 
       } 
       complate = true; 
       byte[] resultByteArray = byteArrayOutputStream.toByteArray(); 
       Byte[] result = new Byte[resultByteArray.length]; 
       for(int i=0; i<result.length; i++) 
        result[i] = resultByteArray[i]; 
       return result; 

      } 
      catch (IOException e) 
      { 
       e.printStackTrace(); 
       Byte[] result = new Byte[1]; 
       result[0] = ERROR_DOWNLOAD_FAILED; 
       return result; 
      } 

메모를 표시하지 않습니다 :이 경우에는 내가 저장하려면 데이터를 바이트 []로 저장하고 파일을 정상적으로 저장 FileOutputStream 및 ... 전체 바이트 []를 얻은 후 다운로드가 완료되었습니다.

tahnks

+1

byteArrayOutputStream.write (버퍼, 0는 newLength); – pskink

+0

오, 네. 고마워 많이 : acceptiong 대답 D에 대한 대답 –

+0

만약 내가 아래에 대답 :) –

답변

1

변화율 :

byteArrayOutputStream.write(buffer); 

행 :

byteArrayOutputStream.write(buffer, 0, newLength); 
관련 문제