【问题标题】:File object can't find the file when there is one文件对象存在时找不到文件
【发布时间】:2013-11-18 05:58:30
【问题描述】:

在我的应用程序中,用户单击按钮后,下载管理器开始从 Internet 下载文件并使用以下代码将其保存到内部 sd 卡:

void startDownload()
    {
        Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).mkdirs();        
        download_req.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
        .setAllowedOverRoaming(false)
        .setTitle(PersianReshape.reshape("Downloading"))
        .setDescription(PersianReshape.reshape("currently downloading the file..."))
        .setDestinationInExternalPublicDir(Environment.getExternalStorageDirectory().getAbsolutePath() , packageId + ".sqlite");
        download_ID = mgr.enqueue(download_req);
    }

下载后,我计划每次使用此代码运行应用程序时检查它的存在性:

String DatabaseAddress =
Environment.getExternalStorageDirectory().getAbsolutePath() +
"/ee.sqlite";
File file = new File(DatabaseAddress);
Log.d("PATH File: ", DatabaseAddress);
if (file.exists()){
Toast.makeText(getApplicationContext(), "found", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(), "not found", Toast.LENGTH_SHORT).show();
}

现在,当我运行此代码时,它会返回“未找到”消息,而文件已经存在(我使用文件管理器检查了它的存在)。

我测试的设备是nexus 7,保存下载文件的路径是:/storage/emulated/0/ee.sqlite

ee.sqlite 是下载文件的文件名。

/storage/emulated/0/ 是app返回的默认路径

为此代码添加到清单的权限是:

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

问:为什么file.exists()有文件时返回false?

更新: 当我使用硬编码时

File temp = new File("/sdcard/storage/emulated/0/", "ee.sqlite");

它有效,但是当我使用 Environment.getExternalStorageDirectory().getPath() 时它不起作用。

【问题讨论】:

  • 尝试打印file.getAbsolutePath():它们(文件)可能位于不同的目录中。另一个可能的原因是权限,但我不认为是因为权限。
  • file.getAbsolutePath 返回与 file.getPath() 相同的地址。
  • 我的意思是下载的目的路径和你检查是否存在的路径可能不同。
  • 顺便说一句,你有没有提到在你的代码中你在一个地方使用 getExternalStoragePublicDirectory() 而在另一个地方使用 getExternalStorageDirectory()?
  • 如果它们相同,Log.d("~~~", "----" + new File(file.getAbsolutePath()).exists()) 应该打印 true 而 Log.d("~~~", "----" + file.exists()) 打印 false。这将是荒谬的,也意味着一个错误。

标签: java android android-networking android-file


【解决方案1】:

只需像这样初始化File

更新: 这只是因为那里缺少分隔符:

 public static File getExternalStorageDirectory() {
    return EXTERNAL_STORAGE_DIRECTORY;
}

而 EXTERNAL_STORAGE_DIRECTORY 是:

private static final File EXTERNAL_STORAGE_DIRECTORY
        = getDirectory("EXTERNAL_STORAGE", "/sdcard");

static File getDirectory(String variableName, String defaultPath) {
    String path = System.getenv(variableName);
    return path == null ? new File(defaultPath) : new File(path);
}

String baseDir = EXTERNAL_STORAGE_DIRECTORY ;
String fileName = "ee.sqlite";

// Not sure if the / is on the path or not
File file = new File(baseDir + File.separator + fileName);
if (file.exists()) {
        Toast.makeText(getApplicationContext(), "found", Toast.LENGTH_SHORT).show(); 
    }
else{
        Toast.makeText(getApplicationContext(), "not found", Toast.LENGTH_SHORT).show();
    }

第二种可能性:当您在模拟器中运行它时,您必须确保启用了外部存储,如下所示:

在 Eclipse 中,转到窗口 > Android SDK 和 AVD 管理器。选择适当的 AVD,然后单击编辑。

确保您已启用 SD 卡支持。如果没有,请单击“新建”按钮并选择“SD 卡支持”选项。

【讨论】:

  • 你是如何给出 dirName 和 fileName 的?
  • 是的,String dirName = Environment.getExternalStorageDirectory().getAbsolutePath();和字符串文件名=“ee.sqlite”;
  • 试过了,还是不行。只有硬编码的字符串有效。
  • 这只是因为分隔符。你没有给那里。我已经在我的更新部分提到它并更新了我的答案
  • 查看我的第二个可能性部分
【解决方案2】:

终于通过改变 File 对象让它工作了。

解决办法是把File对象改成如下:

File file = new File(Environment.getExternalStorageDirectory().getPath() + dirName + File.separator , fileName);

dirName 在哪里:

String dirName = Environment.getExternalStorageDirectory().getAbsolutePath();

文件名是:

String fileName = "ee.sqlite";

所以我可以使用我的条件函数轻松检查文件是否存在。

if (file.exists()){
            Toast.makeText(getApplicationContext(), "found", Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(getApplicationContext(), "not found", Toast.LENGTH_SHORT).show();
        }

我要感谢 @andru 对我的帮助。

【讨论】:

  • 这似乎没有多大意义,您包含两倍于外部存储目录的绝对路径。
猜你喜欢
  • 2014-11-15
  • 1970-01-01
  • 1970-01-01
  • 2012-07-19
  • 2015-12-15
  • 1970-01-01
  • 1970-01-01
  • 2023-03-02
  • 1970-01-01
相关资源
最近更新 更多