2012-05-24 20 views
14

में संपत्ति फ़ोल्डर से डेटाबेस कॉपी करें मैं डीबी को संपत्ति फ़ोल्डर से डिवाइस में कॉपी करने की कोशिश कर रहा हूं। यह कोड एमुलेटर और रूट डिवाइस पर ठीक काम कर रहा है। मैं सिर्फ यह जानना चाहता हूं कि यह अनियंत्रित डिवाइस पर कोई समस्या पैदा करता है या यह वही काम करेगा।अनियंत्रित डिवाइस

private void StoreDatabase() { 
    File DbFile = new File(
      "data/data/packagename/DBname.sqlite"); 
    if (DbFile.exists()) { 
     System.out.println("file already exist ,No need to Create"); 
    } else { 
     try { 
      DbFile.createNewFile(); 
      System.out.println("File Created successfully"); 
      InputStream is = this.getAssets().open("DBname.sqlite"); 
      FileOutputStream fos = new FileOutputStream(DbFile); 
      byte[] buffer = new byte[1024]; 
      int length = 0; 
      while ((length = is.read(buffer)) > 0) { 
       fos.write(buffer, 0, length); 
      } 
      System.out.println("File succesfully placed on sdcard"); 
      // Close the streams 
      fos.flush(); 
      fos.close(); 
      is.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

} 
+1

हाँ अपने कोड स्निपेट unrooted उपकरणों पर पूरी तरह से काम करता है भी :) –

उत्तर

13

यह सभी उपकरणों और एमुलेटर में निश्चित रूप से काम करेगा, रूट की आवश्यकता नहीं है।

/** 
* Copies your database from your local assets-folder to the just created 
* empty database in the system folder, from where it can be accessed and 
* handled. This is done by transfering bytestream. 
* */ 
private void copyDataBase(String dbname) throws IOException { 
    // Open your local db as the input stream 
    InputStream myInput = myContext.getAssets().open(dbname); 
    // Path to the just created empty db 
    File outFileName = myContext.getDatabasePath(dbname); 
    // Open the empty db as the output stream 
    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(); 
} 
+0

मेरा कोड भी ठीक काम कर रहा है मैं सिर्फ यह जानना चाहता हूं कि यह अनियंत्रित डिवाइस पर कोई समस्या पैदा कर रहा है? –

+0

निश्चित रूप से नहीं। उपरोक्त कोड ठीक काम करेगा और आपका भी। –

+13

** हार्डकोड पाथ्स ** कभी नहीं। 'आउटफाइलनाम' मान कई एंड्रॉइड वातावरण जैसे कि द्वितीयक खातों पर गलत होगा। डेटाबेस फ़ाइल के लिए पथ प्राप्त करने के लिए 'getDatabasePath()' का उपयोग करें। – CommonsWare

1

मुझे यकीन नहीं है, लेकिन यह हर डिवाइस पर काम करता है जिस पर मैंने परीक्षण किया है। मैं (यहीं कहीं से) इस विधि चुरा लिया है और यह सामान्य दोनों का बैकअप लेने और पुनर्स्थापित करने के लिए किए गए:

public static void movedb(File srcdb, File destdb) 
{ 
    try 
    { 
     if (Environment.getExternalStorageDirectory().canWrite()) 
     {     
      if (srcdb.exists()) 
      { 
       FileChannel src = new FileInputStream(srcdb).getChannel(); 
       FileChannel dst = new FileOutputStream(destdb).getChannel(); 
       dst.transferFrom(src, 0, src.size()); 
       src.close(); 
       dst.close();      
      } 
      else 
      { 
       //ERROR: "Database file references are incorrect"      
      } 
     } 
     else 
     { 
      //ERROR: "Cannot write to file" 
     } 
    } 
    catch (Exception e) 
    { 
     //ERROR: e.getMessage() 
    } 
} 

तो मैं सिर्फ यह फोन करके बैक अप लेने:

movedb(this, getDatabasePath(getDbName()), new File(Environment.getExternalStorageDirectory(), getDatabaseBackupPath())); 

कहाँ getDatabasePath() और getDatabaseBackupPath() सिर्फ स्ट्रिंग हैं मूल्य

+0

मेरे कोड है भी काम ठीक मैं सिर्फ जानना चाहता हूँ यह unrooted डिवाइस पर किसी भी समस्या पैदा करते है? –

2
/** 
* Copy database file from assets folder inside the apk to the system database path. 
* @param context Context 
* @param databaseName Database file name inside assets folder 
* @param overwrite True to rewrite on the database if exists 
* @return True if the database have copied successfully or if the database already exists without overwrite, false otherwise. 
*/ 
private boolean copyDatabaseFromAssets(Context context, String databaseName , boolean overwrite) { 

    File outputFile = context.getDatabasePath(databaseName); 
    if (outputFile.exists() && !overwrite) { 
     return true; 
    } 

    outputFile = context.getDatabasePath(databaseName + ".temp"); 
    outputFile.getParentFile().mkdirs(); 

    try { 
     InputStream inputStream = context.getAssets().open(databaseName); 
     OutputStream outputStream = new FileOutputStream(outputFile); 


     // transfer bytes from the input stream into the output stream 
     byte[] buffer = new byte[1024]; 
     int length; 
     while ((length = inputStream.read(buffer)) > 0) { 
      outputStream.write(buffer, 0, length); 
     } 

     // Close the streams 
     outputStream.flush(); 
     outputStream.close(); 
     inputStream.close(); 

     outputFile.renameTo(context.getDatabasePath(databaseName)); 

    } catch (IOException e) { 
     if (outputFile.exists()) { 
      outputFile.delete(); 
     } 
     return false; 
    } 

    return true; 
} 
+0

मैंने +1 दिया क्योंकि संदर्भ ऑब्जेक्ट की getDatabasePath() विधि का संयोजन और outputFile.getParentFile()। आपके कोड के mkdirs() ने मेरी समस्या हल की जिसमें डेटाबेस कॉपीिंग विफल रही जब मैंने उपयोग किया हार्ड-कोडेड निर्देशिका पथ जो अन्य उदाहरण दिखाते हैं। –

0
private void copyDataBase(Context context) throws IOException { 

    //Log.i(TAG, "Opening Asset..."); 
    // Open your local db as the input stream 
    InputStream myInput = context.getAssets().open(DBHelper.DATABASE_NAME); 

    // Log.i(TAG, "Getting db path..."); 
    // Path to the just created empty db 
    File dbFile = getDatabasePath(DBHelper.DATABASE_NAME); 

    if (!dbFile.exists()) { 
     SQLiteDatabase checkDB = context.openOrCreateDatabase(DBHelper.DATABASE_NAME, context.MODE_PRIVATE, null); 
     if (checkDB != null) { 
      checkDB.close(); 
     } 
    } 

    //Log.i(TAG, "Getting output stream..."); 
    // Open the empty db as the output stream 
    OutputStream myOutput = new FileOutputStream(dbFile); 

    // Log.i(TAG, "Writing data..."); 
    // 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(); 
} 
संबंधित मुद्दे