【问题标题】:how to check if file is available in internal storage如何检查文件是否在内部存储中可用
【发布时间】:2012-11-03 02:24:22
【问题描述】:

我正在尝试从 Internet 下载文件,它成功了,但现在 我想检查文件是否存在于内部存储中。

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。 如何使用代码检查文件是否存在?

【问题讨论】:

  • 使用File类的exists()方法查看。
  • 请注意这不是内部存储,因为 DownloadManager 无法访问任何应用程序的内部存储。它是特定于包的外部存储。参考:https://developer.android.com/training/data-storage/files/external

标签: android download


【解决方案1】:

假设以下是您的文件路径

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

if ( file.exists() ) 
{
     // Toast File is exists
}
else
{
     // Toast File is not exists
}

【讨论】:

    【解决方案2】:
    File applictionFile =  new File(Environment.getExternalStoragePublicDirectory(
                                        Environment.DIRECTORY_DOWNLOADS)+ "/"+<file-name>);
    
        if(applictionFile != null && applictionFile.exists()){
    
       }
    

    如果文件正在下载到默认的 donwload 目录

    【讨论】:

      猜你喜欢
      • 2018-07-11
      • 1970-01-01
      • 1970-01-01
      • 2016-10-11
      • 2011-10-14
      • 1970-01-01
      • 1970-01-01
      • 2011-02-24
      相关资源
      最近更新 更多