2014-05-01 15 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"); 
    } 
}