Java 文件对象可以';如果有文件,则无法找到该文件

Java 文件对象可以';如果有文件,则无法找到该文件,java,android,android-networking,android-file,Java,Android,Android Networking,Android File,在我的应用程序中,用户单击按钮后,下载管理器开始从internet下载文件,并使用以下代码将其保存到内部sd卡: void startDownload() { Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).mkdirs(); download_req.setAllowedNetworkTypes(DownloadManager.R

在我的应用程序中,用户单击按钮后,下载管理器开始从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();
}
下载后,我计划在每次应用程序运行时使用以下代码检查其存在性:

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();
}
现在,当我运行这段代码时,它返回“notfound”消息,而文件已经存在(我使用文件管理器检查了它的存在)

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

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

/storage/emulated/0/是应用程序返回的默认路径

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

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

它可以工作,但当我使用Environment.getExternalStorageDirectory().getPath()时,它不能工作。只需初始化
文件,如下所示:

更新:
这只是因为此处缺少分隔符:

 public static File getExternalStorageDirectory() {
    return 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();
    }
第二种可能性:当您在emulator中运行它时,必须确保已启用外部存储,如下所示:

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

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


通过更改文件对象,最终使其正常工作

解决方案是将文件对象更改为以下内容:

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

String dirName = Environment.getExternalStorageDirectory().getAbsolutePath();
文件名为:

String fileName = "ee.sqlite";
因此,我可以使用我的criteria函数轻松地检查文件是否存在

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

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

试着打印
文件。getAbsolutePath()
:它们(文件)可能位于不同的目录中。另一个可能的原因是权限,但我不认为这是因为permissions.file.getAbsolutePath返回与file.getPath()相同的地址。我的意思是下载的目标路径和您检查存在的路径可能不同。顺便问一下,您是否提到在代码中使用getExternalStoragePublicDirectory()如果它们相同,
Log.d(“~~~”,“----”+新文件(File.getAbsolutePath()).exists())
应打印为true,而
Log.d(“~~~”,“----+File.exists())应打印为false。这将是荒谬的,也将意味着一个bug。您如何给出dirName和fileName?是的,字符串dirName=Environment.getExternalStorageDirectory().getAbsolutePath();和字符串fileName=“ee.sqlite”;试过了,但还是不行。只有硬编码的字符串才能工作。这只是因为分隔符。你没给我钱。我在更新的部分和更新的回答中提到了这一点。请看我的第二个可能性部分,这似乎没有多大意义,您包含的外部存储目录的绝对路径是外部存储目录绝对路径的两倍。