访问手机内部存储以推送SQLite数据库文件

问题描述 投票:0回答:2

我正在使用 Netbeans 和 java 开发我的 Android 应用程序。当我使用模拟器时,我可以访问文件资源管理器,并通过访问以下路径将 SQLite 数据库插入到设备内存中,

data/data/com.example.helloandroid/database

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

有人可以帮我如何将文件添加到手机内部存储中。谢谢

android
2个回答
10
投票

我认为该设备没有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());
}

0
投票

字符串路径 = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "***.db"); var bytes = File.ReadAllBytes(path);

var fileCopyName = string.Format("/storage/emulated/0/Android/data/com.companyname.demo/Database.db"); File.WriteAllBytes(fileCopyName, bytes);

© www.soinside.com 2019 - 2024. All rights reserved.