2014-05-01 75 views
2

我想獲取SQLite內部存儲數據到SD卡,但每次我得到文件未找到異常。導出SQLite數據到SD卡 - 文件未找到異常

這是類,其中我使用來獲得數據,然後存儲到SD卡,看看下面:

MainActivity.java是我使用我的程序中獲取數據的唯一類。

package com.export.data; 

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_main); 

     Button btn = (Button)findViewById(R.id.btnGetData); 
     btn.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       InputStream myInput; 
       try { 
        myInput = new FileInputStream("/data/data/com.example.parsingsqlite/databases/storeJSON.db");//this is 
        // Set the output folder on the SDcard 
        File directory = new File("/sdcard/Hello"); 
        // Create the folder if it doesn't exist: 
        if (!directory.exists()) 
        { 
         directory.mkdirs(); 
        } 
        // Set the output file stream up: 

        OutputStream myOutput = new FileOutputStream(directory.getPath()+"/storeJSON.db"); 

        // Transfer bytes from the input file to the output file 
        byte[] buffer = new byte[1024]; 
        int length; 
        while ((length = myInput.read(buffer))>0) 
        { 
         myOutput.write(buffer, 0, length); 
        } 
        // Close and clear the streams 
        myOutput.flush(); 

        myOutput.close(); 

        myInput.close(); 

       } catch (FileNotFoundException e) { 
        Toast.makeText(MainActivity.this, "File Not Found", Toast.LENGTH_LONG).show(); 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (IOException e) { 
        Toast.makeText(MainActivity.this, "IOException", Toast.LENGTH_LONG).show(); 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       Toast.makeText(MainActivity.this, "Backup Done Succesfully!", Toast.LENGTH_LONG).show(); 

      } 
     }); 

    } 
} 

注:如果我使用相同的應用程序要檢索,然後獲得,但其數據相同的代碼,如果試圖通過按鈕做水龍頭然後讓FileNotFound例外獲得從其他應用程序的數據。 (像現在這樣)

回答

1

這裏是我的解決方案嘗試一次

File f=new File("/data/data/com.example.parsingsqlite/databases/storeJSON.db"); 
        FileInputStream fis=null; 
        FileOutputStream fos=null; 

        try 
        { 
         fis=new FileInputStream(f); 
         fos=new FileOutputStream("/mnt/sdcard/Hello/storeJSON.db"); 
         while(true) 
         { 
         int i=fis.read(); 
         if(i!=-1) 
         {fos.write(i);} 
         else 
         {break;} 
         } 
         fos.flush(); 
         Toast.makeText(MainActivity.this, "DB dump OK", Toast.LENGTH_LONG).show(); 
        } 
        catch(Exception e) 
        { 
         e.printStackTrace(); 
         Toast.makeText(MainActivity.this, "DB dump ERROR", Toast.LENGTH_LONG).show(); 
        } 
        finally 
        { 
         try 
         { 
         fos.close(); 
         fis.close(); 
         } 
         catch(Exception ioe) 
         {} 
        } 

,不要忘了在你的manifest.xml文件中添加以下permission

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
+0

正在獲取 - 數據庫轉儲錯誤 – Sophie

+0

這意味着'/數據庫'上創建'/數據/data/com.example.parsingsqlite/databases/'路徑仔細檢查 –

+0

其實我正在使用兩個不同的應用程序,一個用於存儲數據到數據庫,第二個用於獲取數據到SD卡,如果我在第一個應用程序中使用我的上述代碼我在那裏存儲數據到SQLite,然後能夠獲取數據到SD卡,在這個應用程序中,我有DBHelper類),但是當我使用第二個應用程序來獲取數據到SD卡獲取FileNotFound(這個程序只有MainActivity,我發佈上面)我希望你明白我的觀點 – Sophie

0

嘗試以下方法:

public void backup() { 
    try { 
     File sdcard = Environment.getExternalStorageDirectory(); 
     File outputFile = new File(sdcard, "MyAppDb.bak"); 

     if (!outputFile.exists()) 
      outputFile.createNewFile(); 

     File data = Environment.getDataDirectory(); 
     File inputFile = new File(data, 
       "data/com.example.myapp/databases/myappdb.sqlite"); 
     InputStream input = new FileInputStream(inputFile); 
     OutputStream output = new FileOutputStream(outputFile); 
     byte[] buffer = new byte[1024]; 
     int length; 
     while ((length = input.read(buffer)) > 0) { 
      output.write(buffer, 0, length); 
     } 
     output.flush(); 
     output.close(); 
     input.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
     //throw new Error("Copying Failed"); 
    } 
}