在Android中的sdcard中检查文件是否存在于文件夹中

问题描述 投票:1回答:3

我正在构建一个Android应用程序,它需要一个空的数据库在sdcard的特定文件夹中。因此,我构建了一个代码,从服务器下载数据库,并以编程方式插入SD卡,工作正常。现在问题是当我从系统运行应用程序时,每次新数据库都通过替换旧数据库来运行。现在我想更改代码,好像文件夹中不存在数据库文件,然后只需要下载并保存在特定文件夹中

我的代码在这里:

     String DownloadUrl = "http://myexample.com/android/timeexample.db";
     String fileName = "timeexample.db";

     DownloadDatabase(DownloadUrl,fileName);

public void DownloadDatabase(String DownloadUrl, String fileName) {
    try {
        File root = android.os.Environment.getExternalStorageDirectory();
        File dir = new File(root.getAbsolutePath() + "/timeexample/databases");
        if(dir.exists() == false){
             dir.mkdirs();  
        }
        URL url = new URL("http://myexample.com/android/timeexample.db");
        File file = new File(dir,fileName);

        long startTime = System.currentTimeMillis();
        Log.d("DownloadManager" , "download beginning");
        Log.d("DownloadManager" , "download url:" +url);
        Log.d("DownloadManager" , "download file name:" + fileName);

        URLConnection uconn = url.openConnection();
        uconn.setReadTimeout(TIMEOUT_CONNECTION);
        uconn.setConnectTimeout(TIMEOUT_SOCKET);

        InputStream is = uconn.getInputStream();

        BufferedInputStream bufferinstream = new BufferedInputStream(is);
        ByteArrayBuffer baf = new ByteArrayBuffer(5000);
        int current = 0;
        while((current = bufferinstream.read()) != -1){
            baf.append((byte) current);
        }
        byte[] buffer = new byte[1024];

        FileOutputStream fos = new FileOutputStream( file);
        fos.write(baf.toByteArray());
        fos.flush();
        fos.close();
        Log.d("DownloadManager" , "download ready in" + ((System.currentTimeMillis() - startTime)/1000) + "sec");

    }

    catch(IOException e) {
        Log.d("DownloadManager" , "Error:" + e);
        e.printStackTrace();
    }   
}

如果每次我这样运行,我的数据都会丢失,因为空数据库正在运行。因此我的要求是如果数据库已经存在,那么就不需要下载了。我已经提到了许多例子来克服这个问题。

android performance android-download-manager
3个回答
1
投票

试试这个:

File extStore = Environment.getExternalStorageDirectory();
File myFile = new File(extStore.getAbsolutePath() + "/book1/page2.db");

if(myFile.exists()){
...
}

1
投票

当目录已存在时,您需要从函数返回

这是你的代码

public void DownloadDatabase(String DownloadUrl, String fileName) {
try {
    File root = android.os.Environment.getExternalStorageDirectory();
    File dir = new File(root.getAbsolutePath() + "/timeexample/databases");
    if(dir.exists() == false){
         dir.mkdirs();  
    }

URL url = new URL("http://myexample.com/android/timeexample.db");
    File file = new File(dir,fileName);

你需要改变它

public void DownloadDatabase(String DownloadUrl, String fileName) {
try {
    File root = android.os.Environment.getExternalStorageDirectory();
    File dir = new File(root.getAbsolutePath() + "/timeexample/databases");
    if(dir.exists() == false){
         dir.mkdirs();  
    }
    else 
      return 

URL url = new URL("http://myexample.com/android/timeexample.db");
    File file = new File(dir,fileName);

0
投票

检查数据库文件是否存在或不使用

File f = new File(Environment.getExternalStorageDirectory()+"/timeexample/databases/timeexample.db");
   if(f.exists())
   { 
     /* do whatever you want */ 
   }

现在有几种类型的存储空间可用于Android设备,我建议你使用

boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String state = Environment.getExternalStorageState();

if (Environment.MEDIA_MOUNTED.equals(state)) {
    // We can read and write the media
    mExternalStorageAvailable = mExternalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
    // We can only read the media
    mExternalStorageAvailable = true;
    mExternalStorageWriteable = false;
} else {
    // Something else is wrong. It may be one of many other states, but all we need
    //  to know is we can neither read nor write
    mExternalStorageAvailable = mExternalStorageWriteable = false;
}
© www.soinside.com 2019 - 2024. All rights reserved.