2017-04-12 1 views
0

현재 안드로이드 응용 프로그램의 루트 디렉토리에서 내용을 읽으려고합니다.Android : 루트 응용 프로그램 폴더에서 내용을 읽습니다.

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

내 코드 : 아래에 명시된 바와 같이 나는 나의 매니페스트에 모든 권한을 구현했습니다

public void copytoFileDestination(){ 

    //get the root path of the application. 
    String rootPath = getFilesDir().getPath() + "/images/"; 
    File destination = new File(rootPath); 

    String imgPath = "/storage/emulated/0/Pictures/somefilename.jpg" 
    File source = new File(imgPath); 

    try{ 
     //copy source location to destination directory 
     copyFile(source, destination); 

     //display all the contents of rootPath! How? Attempt: 
     File[] files = destination.listFiles(); 
     for (int i = 0; i < files.length; i++) 
     { 
      Log.d("Files", "FileName:" + files[i].getName()); 
     } 

    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

    private void copyFile(File sourceFile, File destFile) throws IOException { 
    if (!sourceFile.exists()) { 
     return; 
    } 

    FileChannel source = null; 
    FileChannel destination = null; 
    source = new FileInputStream(sourceFile).getChannel(); 
    destination = new FileOutputStream(destFile).getChannel(); 
    if (destination != null && source != null) { 
     destination.transferFrom(source, 0, source.size()); 
     Log.d("copy file", "complete"); 
    } 
    if (source != null) { 
     source.close(); 
    } 
    if (destination != null) { 
     destination.close(); 
    } 

} 

I 대상으로 소스 (이미지 경로)에서 불과 싶어 사본 (루트 경로) 목적지의 내용을 표시하십시오. 그러나, 대상 파일에 ... 파일이 없음을 의미하는 files.length에서 null 예외가 발생 했습니까? 대상 디렉토리에서 읽을 수 없기 때문입니까?

누군가 나를 밝힐 수 있습니까? 그런데

:

  • destination.exist()는 사실이다.
  • destination.canRead()가 true입니다.

도와주세요.

답변

0

, 내가 나를 위해 작동이 시도

public static void copyFile(File sourceFile, File destFile) throws IOException { 
    if (!sourceFile.exists()) { 
     return; 
    } 

    FileChannel source = null; 
    FileChannel destination = null; 
    source = new FileInputStream(sourceFile).getChannel(); 
    destination = new FileOutputStream(destFile).getChannel(); 
    byte[] var1 = new byte[1024]; 

    int var2; 
    while((var2 = source.read(var1)) > 0) { 
     destination.write(var1, 0, var2); 
    } 

    source.close(); 
    destination.close(); 
} 
관련 문제