我想获取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例外获得从其他应用程序的数据。 (像现在这样)
正在获取 - 数据库转储错误 – Sophie
这意味着'/数据库'上创建'/数据/data/com.example.parsingsqlite/databases/'路径仔细检查 –
其实我正在使用两个不同的应用程序,一个用于存储数据到数据库,第二个用于获取数据到SD卡,如果我在第一个应用程序中使用我的上述代码我在那里存储数据到SQLite,然后能够获取数据到SD卡,在这个应用程序中,我有DBHelper类),但是当我使用第二个应用程序来获取数据到SD卡获取FileNotFound(这个程序只有MainActivity,我发布上面)我希望你明白我的观点 – Sophie