1

그물에서 파일을 다운로드하는 코드입니다. 그것은 SD 카드가 전화와 잘 작동하지만 나는 SD 카드가없는 전화에 그것을 테스트했고 나는 다음과 같은 오류 있어요 :getExternalStoragePublicDirectory를 사용할 때 FileNotFoundException이 발생했습니다.

java.io.FileNotFoundException /storage/emulated/0/Movies/65656s5d.mp4 open failed :ENONET (no such file or directory) 

이 내 코드가 doInBackGround에 있습니다

InputStream input = null; 
OutputStream output = null; 
HttpURLConnection connection = null; 
try { 
    URL url = new URL(sUrl[0]); 
    connection = (HttpURLConnection) url.openConnection(); 
    connection.connect(); 

    // expect HTTP 200 OK, so we don't mistakenly save error report 
    // instead of the file 
    if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) { 
     return "Server returned HTTP " + connection.getResponseCode() + " " 
       + connection.getResponseMessage(); 
    } 

    // this will be useful to display download percentage 
    // might be -1: server did not report the length 
    int fileLength = connection.getContentLength(); 

    // download the file 
    input = connection.getInputStream(); 

    long number = (long) Math.floor(Math.random() * 9000000000L) + 1000000000L; 
    String destPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES) 
      + File.separator+number + ".mp4"; 
    output = new FileOutputStream(destPath); 

    byte data[] = new byte[4096]; 
    long total = 0; 
    int count; 
    while ((count = input.read(data)) != -1) { 
     // allow canceling with back button 
     if (isCancelled()) { 
      input.close(); 
      return null; 
     } 
     total += count; 
     // publishing the progress.... 
     if (fileLength > 0) // only if total length is known 
      publishProgress((int) (total * 100/fileLength)); 
     output.write(data, 0, count); 
    } 
    return "ok"; 
} catch (Exception e) { 
    Log.v("this",e.getMessage()); 
    return e.toString(); 
} finally { 
    try { 
     if (output != null) 
      output.close(); 
     if (input != null) 
      input.close(); 
    } catch (IOException ignored) { 
     Log.v("this",ignored.getMessage()); 
     return ignored.toString(); 
    } 

    if (connection != null) 
     connection.disconnect(); 
} 

이 문제를 어떻게 해결할 수 있습니까?

+0

해당 기기에서 Android 6.0이 실행되고 있습니까? – greenapps

+0

'그물에서 파일을 다운로드하기위한이 코드 .' 관련이 없습니다. 코드가 파일 시스템에 파일을 만들려고합니다. – greenapps

답변

-1

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES) 장치에도 sd 카드가 없어도 영향을 미치지 않습니다. 항상 존재합니다. 파일이 존재하는지 확인하십시오 (존재하는 방법을 사용하여 File 클래스에)?