2012-09-06 1 views
-1

나는이 이상한 문제에 휩싸였다. 이 코드를 사용하여 MP3 파일을 다운로드 할 때다운로드 jpg, apk 파일은 열리지 만 mp3 파일은 열 수 없음

public boolean downloadSection(String link,String fileLocation,long currentlyDownloadedBytes){ 

    try { 

       RandomAccessFile file = new RandomAccessFile(fileLocation, "rw"); 
       file.seek(currentlyDownloadedBytes+1); 

       URL imageUrl = new URL(link); 
       HttpURLConnection conn =(HttpURLConnection)imageUrl.openConnection(); 
       conn.setRequestProperty("Range", "bytes=" + currentlyDownloadedBytes + "-"); 
       conn.setConnectTimeout(100000); 
       conn.setReadTimeout(100000); 
       conn.setInstanceFollowRedirects(true); 

       InputStream is=conn.getInputStream(); 
       byte buffer[]=new byte[1024];; 
       while (true) { 
        // Read from the server into the buffer 
        int read = is.read(buffer); 
        if (read == -1) { 
         break; 
        } 
        // Write buffer to file 
        file.write(buffer, 0, read); 
       } 

       file.close(); 
       return true; 
       } catch (Exception ex){ 
         ex.printStackTrace(); 
         return false; 
         } 

} 

가 downloades가 열어 파일, 그러나 다운로드 안드로이드 응용 프로그램 (의 .apk 파일) 을 제공합니다 : 나는 파일을 다운로드하려면 다음 방법을 사용하고

패키지 구문 분석 오류이 열리고 이미지가으로 열리지 않습니다. 도와주세요

답변

0

여러 가지가 이상하게 보일 감사 :

A) 링크 유형이 없기 때문에 당신이 그것을 그래서 (이 질문에 대한 수동 방법의 서명을 추가 한 것 같습니다 심지어 컴파일되지 않습니다).

b) alreadyDownloadedBytes 및 currentlyDownloadedBytes라는 두 개의 변수/매개 변수가 있습니다. 나는 당신은

file.seek(currentlyDownloadedBytes+1); 

다음과 같은 일을) # A

C에 의한 것 가정 그리고 당신은 대신 오프셋을 추구하기 때문에이

file.seek(currentlyDownloadedBytes); 

이유는해야한다. 예를 들어, 1 바이트 만 읽으면 1을 오프셋해야합니다 (파일의 두 번째 바이트가 됨).

d) 예외가 던져 졌는지 확인하십시오.

e) currentlyDownloadedBytes 또는 alreadyDownloadedBytes는 어디에 업데이트합니까?

F) 내 최종 추천 응용 프로그램을 디버깅하고 내가 mannually 코드를 만들 수있는 서명을 추가 한 전체 파일 내가이 내 코드의 사촌을 설명에서 변경 한 어리석은 실수

+0

죄송 다운로드되어 있는지 확인하는 것입니다 여기에 설명해. 그리고 찾는 동안 currentlyDownloadedBytes에 1을 더하면 문제가 발생합니다. 모두 잘 지금 :) –