【问题标题】:Access the phone internal storage to push in SQLite database file访问手机内部存储推送 SQLite 数据库文件
【发布时间】:2011-09-01 10:14:44
【问题描述】:

我正在使用 Netbeans 和 java 开发我的 android 应用程序。当我使用模拟器时,我可以访问文件资源管理器并将 SQLite 数据库插入到设备内存中,方法是访问以下路径 data/data/com.example.helloandroid/database

但是当我使用真实设备时,我无法访问此位置以将 SQLite 文件推送到手机的内部存储(位置)。

谁能帮助我如何将文件添加到手机内部存储中。谢谢

【问题讨论】:

  • 您不能将数据库文件保存在资产文件夹中并以编程方式复制到文档文件夹吗?
  • 我可以在使用模拟器时简单地推入数据库文件,使用 DDMS -> 文件资源管理器。使用真机时我需要这样做。
  • 好的,看看我编辑的答案。 :-)

标签: android


【解决方案1】:

我认为该设备没有root权限,这就是您无法访问它的原因。如果您想以编程方式在您的应用程序中进行操作,那么这是可能的。如果有人知道,请分享一下。

编辑:好的,首先,

1. copy your Database.db file in your projects assets folder.
2. now using code copy database file from /asset to device's internal storage 
   (data/data/<package name>/database folder).

复制文件使用下面的代码,

try {
     // Open your local db as the input stream
     InputStream myInput = myContext.getAssets().open("your database file name");

     // Path to the just created empty db
     String outFileName = "/data/data/<your_app_package_name>/databases/<database_file_name>";

     OutputStream myOutput = new FileOutputStream(outFileName);

     // transfer bytes from the inputfile to the outputfile
     byte[] buffer = new byte[1024];
     int length;
     while ((length = myInput.read(buffer)) > 0) 
         {
       myOutput.write(buffer, 0, length);
     }

    // Close the streams
    myOutput.flush();
    myOutput.close();
    myInput.close();
} 
catch (Exception e) 
{
     Log.e("error", e.toString());
}

【讨论】:

  • 您能否解释一下,如何以编程方式将 sqlite 数据库文件移动到手机内存中。
  • 您为什么要这样做?适合您的应用程序?
  • 我创建了一个包含许多记录的 SQLite 数据库。(读取一些 xml 文件)。所以这个完整的应用程序适用于 SQLite 数据库记录。我需要用真机测试它并卡住了..!!!!
  • 一切正常,但请澄清您正在处理您的应用程序并且您正在使用此数据库。现在您正在真实设备上对其进行测试,对吗?
  • @Abhilash - 代替硬编码路径 /data/data/ 使用 public abstract File getDatabasePath (String name) 方法。
猜你喜欢
  • 2015-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-14
  • 1970-01-01
  • 2020-05-03
  • 2017-12-19
  • 2018-07-06
相关资源
最近更新 更多