2010-07-02 5 views
0

웹 서비스를 사용하여 클라우드에서 버퍼 저장소로 파일을 다운로드 할 수 있습니다. 우리는 이제 버퍼 메모리에서이 다운로드 파일에 액세스하여 SD 카드로 옮길 필요가 있습니다. Eclipse와 Android 플러그인을 사용하고 있습니다.android 에뮬레이터에서 파일을 다운로드하는 방법은 무엇입니까?

코드를 공유 할 수 있습니까?

내 코드 단편은 아래에 있습니다.

 String filename = "Test.zip"; 
     URL url = new URL(FromUrl); 
     HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 
     urlConnection.setRequestMethod("GET"); 
     urlConnection.setDoOutput(true); 
     urlConnection.connect(); 

     FileOutputStream fileOutput = openFileOutput(filename, Context.MODE_WORLD_READABLE); 

     InputStream inputStream = urlConnection.getInputStream(); 

     int totalSize = urlConnection.getContentLength(); 
     int downloadedSize = 0; 

     //create a buffer... 
     byte[] buffer = new byte[1024]; 
     int bufferLength = 0; //used to store a temporary size of the buffer 

     //now, read through the input buffer and write the contents to the file 
     while ((bufferLength = inputStream.read(buffer)) > 0) { 
      fileOutput.write(buffer, 0, bufferLength); 
      downloadedSize += bufferLength; 
     } 
     //close the output stream when done 
     fileOutput.close(); 

     return filename; 

답변

0

그냥 sdcard에 저장 하시겠습니까?

fileoutput = new FileOutputStream("/sdcard/"+filename); 

이것은 빠르고 빠르지 만, 지금까지 발견 된 모든 안드로이드에서 작동합니다. 조금 더 깔끔하고 싶다면 ...

File ofile = new File(Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_DOWNLOADS),filename); 
FileOutputStream f = new FileOutputStream(ofile); 
관련 문제