2013-07-18 2 views
0

인터넷에서 파일을 다운로드하여 SDCard에 저장해야하는 APP를 개발 중입니다. "파싱 오류"와 같이 다운로드하는 동안 일부 장치에서 오류를보고합니다. 일부 장치에는 SDCard가 없거나 클래스에 들어있는 경로가 올바르지 않다고 가정합니다. SDCard가 없거나 마운트되지 않은 경우 모든 장치를 지원하는 가장 안전한 방법은 무엇입니까? 이 내 코드입니다 :Android - 파일을 인터넷에서 SDCard로 다운로드 - 가장 안전한 방법

OutputStream output = new FileOutputStream(Environment.getExternalStorageDirectory().getPath()+"/Download/file.apk"); 

내가 getExternalStorageDirectory을 (사용해야) 및를 다운로드 :

/** 
     * Downloading file in background thread 
     * */ 
     @Override 
     protected String doInBackground(String... f_url) { 
      int count; 
      try { 
       URL url = new URL(f_url[0]); 
       URLConnection conection = url.openConnection(); 
       conection.connect(); 
       // getting file length 
       int lenghtOfFile = conection.getContentLength(); 

       // input stream to read file - with 8k buffer 
       InputStream input = new BufferedInputStream(url.openStream(), 8192); 

       // Output stream to write file 
       OutputStream output = new FileOutputStream(Environment.getExternalStorageDirectory().getPath()+"/Download/file.apk"); 

       byte data[] = new byte[1024]; 

       long total = 0; 

       while ((count = input.read(data)) != -1) { 
        total += count; 
        // publishing the progress.... 
        // After this onProgressUpdate will be called 
        publishProgress(""+(int)((total*100)/lenghtOfFile)); 

        // writing data to file 
        output.write(data, 0, count); 
       } 

       // flushing output 
       output.flush(); 

       // closing streams 
       output.close(); 
       input.close(); 

      } catch (Exception e) { 
       Log.e("Error: ", e.getMessage()); 
      } 

      return null; 
     } 

내가 문제가이 라인에있을 수 있습니다 생각하십니까? 또는 모든 장치에 공통적 인 "가장 안전한"위치가 있습니까?

답변

1

먼저 AsyncTask를 사용하여 파일을 다운로드하지 않으려합니다. 사용자가 작업을 호스팅하는 화면을 죽이면 다운로드도 종료됩니다. IntentService를 조사하십시오.

둘째, 여기에 안드로이드 코드 예제를 숙지 : http://developer.android.com/guide/topics/data/data-storage.html#filesExternal

당신은 적절한 디렉토리를 얻을 후 가능한 것을 확인 할 수 있습니다.

관련 문제