2016-06-26 8 views
0
public void saveByteArrayToFile(Context context){ 
     Log.v(TAG, "Save byte array to file"); 
     switch(mType){ 
      case ChatMessage.AUDIO_MESSAGE: 
       filePath = context.getApplicationContext().getExternalFilesDir(Environment.DIRECTORY_MUSIC).getAbsolutePath()+"/"+fileName; 
       break; 
      case ChatMessage.VIDEO_MESSAGE: 
       filePath = context.getApplicationContext().getExternalFilesDir(Environment.DIRECTORY_MOVIES).getAbsolutePath()+"/"+fileName; 
       break; 
      case ChatMessage.FILE_MESSAGE: 
       filePath = context.getApplicationContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath()+"/"+fileName; 
       break; 
      case ChatMessage.DRAWING_MESSAGE: 
       filePath = context.getApplicationContext().getExternalFilesDir(Environment.DIRECTORY_PICTURES).getAbsolutePath()+"/"+fileName; 
       break; 
     } 
     System.out.println("Mera path"+filePath); 
     File file = new File(filePath); 

     if (file.exists()) { 
      file.delete(); 
     } 

     try { 
      FileOutputStream fos=new FileOutputStream(file.getPath()); 

      fos.write(byteArray); 
      fos.close(); 
      Log.v(TAG, "Write byte array to file DONE !"); 
     } 
     catch (java.io.IOException e) { 
      e.printStackTrace(); 
      Log.e(TAG, "Write byte array to file FAILED !"); 
     } 
    } 

java.io.FileNotFoundException :/저장/sdcard0/안드로이드/데이터/... 에 의해 발생 : android.system.ErrnoException : 실패 열지 : ENOENT (없음 같은 파일 또는 디렉토리)오류가

파일을 만드는 중에이 오류가 발생합니다.

+0

에 설명 된대로, SDK 버전을 대상으로하는 경우 그 길에 쓴다 고요? – LEQADA

답변

0

따라서 파일이 존재합니다. 그리고 이런 : 지금

if (file.exists()) { 
    file.delete(); 
} 

존재하지 않는, 당신이 사용할 수 있도록 :

file.getPath(); 

을 대신 이것을 사용 :

FileOutputStream fos=new FileOutputStream(filePath); 
0

확실하지 당신은 이미 완료 한 경우 이 경우 앱이 매니페스트의 외부 저장소에 글을 쓸 것이라고 선언해야합니다 (apk 버전 23 이상을 타겟팅하는 경우 앱에 권한을 부여하기 전에 사용자에게 허용 여부를 요청해야합니다).

매니페스트에서 응용 프로그램 태그 안에이 줄을 추가

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

런타임 권한을 요청해야 23+ 아마 당신이 할 수있는 권한이 없습니다 https://developer.android.com/training/permissions/requesting.html