5

インターネットからファイルをダウンロードしようとして成功しましたが、ファイルが内部ストレージに存在するかどうかを確認したいと思います。

else if (arg0.getId() == R.id.btn_download)
{
    Toast.makeText(this, "download button clicked", Toast.LENGTH_SHORT).show();

    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(names[b]));
    request.setDescription("Downloading..");
    request.setTitle("Futsing Magazine Issue " + (this.mPictureManager.getCurrentIndex() +1) );
    // in order for this if to run, you must use the android 3.2 to compile your app
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        request.allowScanningByMediaScanner();
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    }
    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "Futsing Magazine Issue " + (this.mPictureManager.getCurrentIndex()
            +1) +".pdf");

    // get download service and enqueue file
    DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
    manager.enqueue(request);

取得したアイテムは /mnt/sdcard/Download にダウンロードされます。ファイルが存在するか、コードを使用していないかを確認するにはどうすればよいですか?

4

2 に答える 2

13

以下がファイルのパスであるとしましょう

String path=context.getFilesDir().getAbsolutePath()+"/filename";
File file = new File ( path ); 

if ( file.exists() ) 
{
     // Toast File is exists
}
else
{
     // Toast File is not exists
}
于 2012-11-03T02:31:11.607 に答える