2017-06-28 50 views
-2

我想檢查位於我的原始文件夾下我的水庫的MP3文件。我的MP3文件存在,但我的代碼給了我一個虛假的回報。檢查一個文件,如果它存在或不存在(不工作)

String filename = "android.resource://" + this.getPackageName() + "/raw/w"; 

Toast.makeText(ViewPager.this, mp3check(filename)+"",Toast.LENGTH_LONG).show(); 

檢查MP3文件

public boolean mp3check(String _filename) { 
     return new File(_filename).exists(); 
    } 

回答

0

試試這個

String filename = "android.resource://" + this.getPackageName() + "/raw/w"; 
    String newFilePath = "new file path"; // ExternalStorageDirectory path 
    final Path destination = Paths.get(newFilePath); 
    try (
     final InputStream in = getResources().openRawResource(R.raw.w); 
    ) { 
     Files.copy(in, destination); 
    } 

Toast.makeText(ViewPager.this, mp3check(filename)+"",Toast.LENGTH_LONG).show(); 


    public boolean mp3check(String _newFilePath) { 
      return new File(_newFilePath).exists(); 
     } 
+0

我正在使用API​​ 14,但是這行最終路徑destination = Paths.get(newFilePath);'所需的API 26。 –

1

1)我不認爲你可以把原始資源作爲一個普通的文件。 2)由於它是一種資源,因此不需要檢查它是否存在。 3)您可以使用資源ID打開它。 例如,如果你的文件被命名爲,mysong.mp3,你可以像這樣打開它:

InputStream is = getResources().openRawResource(R.raw.mysong); 

AssetFileDescriptor afd = getResources().openRawResourceFd(R.raw.mysong); 

您可以使用AssedFileDescriptor用的MediaPlayer播放。

相關問題