【问题标题】:copy database file to sdcard in android在android中将数据库文件复制到sdcard
【发布时间】:2013-10-06 06:30:44
【问题描述】:

我通过这段代码获取我的数据库文件

  File dbFile=getDatabasePath("EdsysEyfsDB.db");
               Log.v("database name checking", dbFile.toString());

我想将此数据库文件复制到 sdcard,以便我可以为此进行一些操作。但我无法对此进行任何操作。以下代码用于复制到 sd 卡

         if (dbFile.exists()) {
                       InputStream inStream = new FileInputStream(dbFile);
                       String file = Environment.getExternalStorageDirectory().getPath()
                            +"/" + "database.db";
                       Log.d("file name checking in  dbFilecondition", file);
                       FileOutputStream fs = new FileOutputStream(file);
                       byte[] buffer = new byte[1444];
                       while ((byteread = inStream.read(buffer)) != -1) {
                           bytesum += byteread;
                           fs.write(buffer, 0, byteread);
                       }
                       inStream.close();
                       fs.close();
                   }

但我不会在这种情况下。数据库文件名在 LogCat 上正确出现。我已经授予读写文件的权限。

【问题讨论】:

    标签: android database file


    【解决方案1】:

    试试这个希望对你有帮助

    public void exportDatabse(String databaseName) {
            try {
                File sd = Environment.getExternalStorageDirectory();
                File data = Environment.getDataDirectory();
    
                if (sd.canWrite()) {
                    String currentDBPath = "//data//"+getPackageName()+"//databases//"+databaseName+"";
                    String backupDBPath = "backupname.db";
                    File currentDB = new File(data, currentDBPath);
                    File backupDB = new File(sd, backupDBPath);
    
                    if (currentDB.exists()) {
                        FileChannel src = new FileInputStream(currentDB).getChannel();
                        FileChannel dst = new FileOutputStream(backupDB).getChannel();
                        dst.transferFrom(src, 0, src.size());
                        src.close();
                        dst.close();
                    }
                }
            } catch (Exception e) {
    
            }
        }
    

    如何拨打电话

    exportDatabse("YourDBName");
    

    注意:

    记得添加写入外部存储的权限 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />,否则 sd.canWrite() 将为 false。

    【讨论】:

    • 记得用<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />添加写入外部存储的权限,否则sd.canWrite()会是假的。
    • 它可以在 Android 9 上运行吗?我有 Android 8 和 Android 9 设备,此代码不适用于 Android 9 设备。没有例外。代码工作正常,但没有任何反应。数据库文件未更改。
    • kolombo,这部分代码不再相关。在下面检查我的答案。它适用于装有 Android 9.0 的 Galaxy S9
    【解决方案2】:

    不幸的是,接受的答案与这些天无关。问题出在错误检测到的 SD 路径上。实际路径通常取决于移动设备的制造商,并且可能因型号而异。

    我想出了一个简单的解决方案,可以独立于 Android 版本检测到 SD 的实际路径,ContextCompat.getExternalFilesDirs[1] 包含一个相关字符串。在安卓 6 到 9 版本的设备上测试

    第二个问题是定义了 DB 的路径。它应该在包名之前包含“data/data/”,例如:“/data/data/” + getPackageName() + “/databases/” + dbFilename;

    这是我项目中的部分代码

    String sdPath;
    
        private boolean isSDPresent(Context context) {
    
            File[] storage = ContextCompat.getExternalFilesDirs(context, null);
            if (storage.length > 1 && storage[0] != null && storage[1] != null) {
                sdPath = storage[1].toString();
                return true;
            }
            else
              return false;
        }
    
    
        private boolean isContextValid(Context context) {
            return context instanceof Activity && !((Activity) context).isFinishing();
        }
    
        public boolean exportDatabase(Context context, String localDbName, String backupDbName) {
            if (isContextValid(context))
                try {
                    if (!SettingsFile.isSDPresent(context)) {
                        Log.e(TAG, "SD is absent!");
                        return false;
                    }
    
                    File sd = new File(sdPath); 
    
                    if (sd.canWrite()) {
    
                        File currentDB = new File("/data/data/" + context.getPackageName() +"/databases/", localDbName); 
                        File backupDB = new File(sd,  backupDbName);
    
                        if (currentDB.exists()) {
                            FileChannel src = new FileInputStream(currentDB).getChannel();
                            FileChannel dst = new FileOutputStream(backupDB).getChannel();
                            dst.transferFrom(src, 0, src.size());
                            src.close();
                            dst.close();
                        }
                    }
                    else {
                        Log.e(TAG, "SD can't write data!");
                        return false;
                    }
                } catch (Exception e) {
    
                }
            else {
                Log.e(TAG, "Export DB: Context is not valid!");
                return false;
            }
    
            return true;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-29
      • 2014-01-30
      • 1970-01-01
      • 1970-01-01
      • 2022-08-02
      • 1970-01-01
      相关资源
      最近更新 更多