2014-12-10 3 views
0

여기에서 문제가 무엇인지 알 수 없으므로 더 이상 작동하지 않습니다. URL에서 파일을 다운로드하려고합니다. 파일을 제대로 만들지 못하는 것 같습니다.파일을 다운로드 할 때 Java.io.FileNotFoundException이 발생합니다.

  static String PATH = Environment.getExternalStorageDirectory() + "/Android/data/com.xxxx.xxxx/"; 
      url = new URL("http://xxxxxxx.xxx/file.apk"); 
      HttpURLConnection c = (HttpURLConnection) url.openConnection(); 
      c.setRequestMethod("GET"); 
      c.setDoOutput(false); 
      c.connect(); 


      File file = new File(PATH); 
      File outputFile = new File(file, "update"+separated[1]+".apk"); 

      //it crashes at this point 

      FileOutputStream fos = new FileOutputStream(outputFile); 

      InputStream is = c.getInputStream(); 

      byte[] buffer = new byte[1024]; 
      int len1 = 0; 
      while ((len1 = is.read(buffer)) != -1) { 
       fos.write(buffer, 0, len1); 
      } 
      fos.close(); 
      is.close(); 


java.io.FileNotFoundException: /storage/emulated/0/Android/data/com.xxxxx.xxx/update150.apk: open failed: ENOENT (No such file or directory) 
    at libcore.io.IoBridge.open(IoBridge.java:409) 
    at java.io.FileOutputStream.<init>(FileOutputStream.java:88) 
    at java.io.FileOutputStream.<init>(FileOutputStream.java:73) 
    at com.sanisidrolsa.appterris.GcmIntentService.onHandleIntent(GcmIntentService.java:67) 
    at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:136) 
    at android.os.HandlerThread.run(HandlerThread.java:61) 
Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory) 
    at libcore.io.Posix.open(Native Method) 
    at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110) 
    at libcore.io.IoBridge.open(IoBridge.java:393) 
    ... 7 more 
+1

올바른 위치에 파일이 올바르게 다운로드되고 파일 이름이 올바른지 수동으로 확인 했습니까? 두 번 확인해주세요. –

+0

'file.mkdirs();'아마도 디렉토리를 만들 수 있을까요? –

+0

변경 FileOutputStream fos = 새 FileOutputStream (출력 파일); OutputStream에 fos = 새로운 FileOutputStream (출력 파일); –

답변

0

는 안드로이드 4.0에서, 당신은 그렇지 않으면 당신은 ANR (응용 프로그램이 응답하지 않음) 오류가 발생, 주 스레드에서 네트워크 또는 HTTP 요청을 할 수 없습니다.

HTTP 요청을 새 스레드에 넣어야합니다. Android OS의 높은 버전은 네트워크 나 데이터베이스와 같은 비동기 I/O 요청이 더 나은 것으로 간주하기 때문에.

Android OS에서 비동기 작업에 대한 여러 가지 기술이 있습니다.

this이 도움이 될 수 있습니다.

+0

사실 모든 것은 인 텐트 서비스 – MauriF

+0

@ MaauriF 하하에 의해 수행됩니다. 타사 라이브러리가 더 편리합니다. https://github.com/loopj/android-async-http –

관련 문제