Android中数据只能存储到sd卡上,将Sugar ORM数据库存储在SD卡而不是默认路径Android中...

我在android中使用sugar orm将数据存储在sqlite数据库中,它工作得很好,所以现在我希望将数据存储在本地存储中,而不是默认路径中,这样我如何才能实现这一点,而且这样做是可能的

谢谢。

这是我的主要活动代码

public class MainActivity extends AppCompatActivity {

EditText firstname;

EditText lastname;

Button button;

Note note;

public SQLiteDatabase database;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

firstname=findViewById(R.id.edit1);

lastname=findViewById(R.id.edit2);

button=findViewById(R.id.button);

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.menu_main, menu);

return true;

}

@Override

public boolean onOptionsItemSelected(MenuItem item) {

int id = item.getItemId();

return id == R.id.action_settings || super.onOptionsItemSelected(item);

}

public void click(View view) {

String first = firstname.getText().toString();

String last = lastname.getText().toString();

note = new Note(first, last);

note.save();

if (note.getFirstname() != null && note.getLastname() != null) {

firstname.setText("");

lastname.setText("");

}

onShareDb();

//Log.e("Notes saved", String.valueOf(onShareDb()));

}

public void show(View view) {

String one=note.getFirstname();

String two=note.getLastname();

Log.e("firstName",one);

Log.e("lastName",two);

}

public void update(View view) {

note = Note.findById(Note.class, 4);

Log.e("firstName",note.getFirstname());

note.setFirstname("kullu");

Log.e("firstName",note.getFirstname());

note.save();

}

public void delete(View view) {

note = Note.findById(Note.class, 2);

if(note.getId()==null){

Toast.makeText(this,"there is no such data",Toast.LENGTH_SHORT).show();

}

Log.e("firstName",note.getFirstname());

note.delete();

Log.e("firstName",note.getFirstname());

}

public void onShareDb() {

@SuppressLint("SimpleDateFormat") SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");

String output_name = "YourApp_" + df.format(new Date()) + ".db";

File output_file = new File(getExternalCacheDir() + File.separator + output_name);

try {

File file = new File(new SugarDb(MainActivity.this).getDB().getPath()); // get private db reference

if (!file.exists() || file.length() == 0) throw new Exception("Empty DB");

//IOUtils.copy(new FileInputStream(file), new FileOutputStream(output_file));

/* Intent i = new Intent(Intent.ACTION_SEND);

i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(output_file));

startActivity(Intent.createChooser(i, "Send db"));*/

database = SQLiteDatabase.openDatabase(output_file

+ File.separator + "notes.db", null,

SQLiteDatabase.OPEN_READWRITE);

Log.e("storage", String.valueOf(database));

} catch (Exception e) {

Toast.makeText(getApplicationContext(), "Unable to export db: " + e.getMessage(), Toast.LENGTH_SHORT).show();

Log.e("storage", e.getMessage());

}

}

}

所以,基本上,我试图通过使用sugar orm的sharedb()属性来获取存储图像的路径,并试图覆盖到我的新路径的默认路径,所以如何完成它,我在button click listener中调用sharedb方法,异常类似于未知错误:无法打开数据库。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值