i am using sugar orm to store my data in sqlite database in android and it is working perfectly so now i want to store the data in the local storage rather than the default path so how can i achieve that and moreover that is it possible to do this Thanks. This is my mainactivity code
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());
}
}
}
So, basically i m trying to get the path of stored images by using the shareDB() property of sugar orm and trying to overwrite the default path to my new path so how do i get it done, i m calling shareDB method in button click listener, the exception is something like unknown error: could not open database.
After a lot of research and trial error, I somehow manage to succeed in copying the sqllite file from one folder to another folder in the directory
Here is the code,
private void copyDatabase() throws IOException {
File actualFile = new File(new SugarDb(MainActivity.this).getDB().getPath());
File cuurentfile = new File(actualFile.toString());
Log.e("actualPath", actualFile.toString());
File newFile = createTempFile("sugarFiles",".db",Environment.getExternalStorageDirectory());
Log.e("newPath", newFile.toString());
boolean yes=FileUtils.copyFile(cuurentfile,newFile);
if(yes) {
Log.e("result", "" + true);
}
}
call this copydatabase function inside the click listener or wherever you are inserting into the database, make sure it is after you set the insertion values, in my case
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("");
}
try {
copyDatabase();
} catch (IOException e) {
e.printStackTrace();
}
}
FileUtils.java
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
public class FileUtils {
FileUtils instance = null;
public FileUtils getInstance() {
instance = new FileUtils();
return instance;
}
public static Boolean copyFile(File sourceFile, File destFile)
throws IOException {
// if (!destFile.exists()) {
destFile.createNewFile();
FileChannel source = null;
FileChannel destination = null;
try {
source = new FileInputStream(sourceFile).getChannel();
destination = new FileOutputStream(destFile).getChannel();
destination.transferFrom(source, 0, source.size());
} finally {
if (source != null)
source.close();
if (destination != null)
destination.close();
}
return true;
// }
// return false;
}
/**
* Read a text file into a String.
*
* @param file
* File to read (will not seek, so things like /proc files are
* OK).
* @return The contents of the file as a String.
* @throws IOException
*/
public static String readTextFile(File file) throws IOException {
byte[] buffer = new byte[(int) file.length()];
BufferedInputStream stream = new BufferedInputStream(
new FileInputStream(file));
stream.read(buffer);
stream.close();
return new String(buffer);
}
}
Hope it helps someone someday...Have a nice day