将SQLite数据导出到SD卡 - 找不到文件异常

时间:2014-05-01 07:17:39

标签: android sqlite

我正在尝试将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异常。 (就像现在这样做)

2 个答案:

答案 0 :(得分: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)
                      {}
                    }

并且不要忘记在permission文件

中添加以下manifest.xml
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

答案 1 :(得分: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");
    }
}