Access the phone internal storage to push in SQLite database file

后端 未结 1 983
夕颜
夕颜 2020-12-03 19:59

I am developing my android application using Netbeans and java. When I am using the emulator I can access the File explorer and insert an SQLite database in to device intern

相关标签:
1条回答
  • 2020-12-03 20:44

    I think the device doesn't have root permission, that's why you can't access it. If you want to do in your app with programmatically then it is possible. If anybody knows better then this please share it.

    EDIT: ok, first of all,

    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).
    

    For copy file use below code,

    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 讨论(0)
提交回复
热议问题