creating a SQLite Backup on root of SD card












0














How can I backup my SQLiteDatabase on the root of my SD card instead of com.jti.mikee.jti_pos. because the problem that I've encountering right now is when i uninstall the app. the package name on SD Card was removed. even the sql backups. I'm developing on a device android 4.4



This is my code for back up a database:



private void DBBackup() {
try {
File sd = getContext().getExternalFilesDirs(null);
File data = Environment.getDataDirectory();

if (sd[1].canWrite()) {
String currentDBPath = "/data/com.jti.mikee.jti_pos/databases/" + DBHelper.DB_NAME;
String backupDBPath =DBHelper.DB_NAME;
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd[1], backupDBPath);

if (currentDB.exists()) {
FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel dst = new FileOutputStream(backupDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
}
}
} catch (Throwable e) {
e.printStackTrace();
}
}









share|improve this question






















  • Of course that apps private folder will get removed, if the app is deinstalled. That is the point of deinstalling it. You certainly can copy any files you like to the sdcard, but they will be visible and everyone can tamper with those files in the public space. You can use normal file handling functions as offered by android/java for that, this works just fine.
    – arkascha
    Nov 21 at 6:05
















0














How can I backup my SQLiteDatabase on the root of my SD card instead of com.jti.mikee.jti_pos. because the problem that I've encountering right now is when i uninstall the app. the package name on SD Card was removed. even the sql backups. I'm developing on a device android 4.4



This is my code for back up a database:



private void DBBackup() {
try {
File sd = getContext().getExternalFilesDirs(null);
File data = Environment.getDataDirectory();

if (sd[1].canWrite()) {
String currentDBPath = "/data/com.jti.mikee.jti_pos/databases/" + DBHelper.DB_NAME;
String backupDBPath =DBHelper.DB_NAME;
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd[1], backupDBPath);

if (currentDB.exists()) {
FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel dst = new FileOutputStream(backupDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
}
}
} catch (Throwable e) {
e.printStackTrace();
}
}









share|improve this question






















  • Of course that apps private folder will get removed, if the app is deinstalled. That is the point of deinstalling it. You certainly can copy any files you like to the sdcard, but they will be visible and everyone can tamper with those files in the public space. You can use normal file handling functions as offered by android/java for that, this works just fine.
    – arkascha
    Nov 21 at 6:05














0












0








0







How can I backup my SQLiteDatabase on the root of my SD card instead of com.jti.mikee.jti_pos. because the problem that I've encountering right now is when i uninstall the app. the package name on SD Card was removed. even the sql backups. I'm developing on a device android 4.4



This is my code for back up a database:



private void DBBackup() {
try {
File sd = getContext().getExternalFilesDirs(null);
File data = Environment.getDataDirectory();

if (sd[1].canWrite()) {
String currentDBPath = "/data/com.jti.mikee.jti_pos/databases/" + DBHelper.DB_NAME;
String backupDBPath =DBHelper.DB_NAME;
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd[1], backupDBPath);

if (currentDB.exists()) {
FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel dst = new FileOutputStream(backupDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
}
}
} catch (Throwable e) {
e.printStackTrace();
}
}









share|improve this question













How can I backup my SQLiteDatabase on the root of my SD card instead of com.jti.mikee.jti_pos. because the problem that I've encountering right now is when i uninstall the app. the package name on SD Card was removed. even the sql backups. I'm developing on a device android 4.4



This is my code for back up a database:



private void DBBackup() {
try {
File sd = getContext().getExternalFilesDirs(null);
File data = Environment.getDataDirectory();

if (sd[1].canWrite()) {
String currentDBPath = "/data/com.jti.mikee.jti_pos/databases/" + DBHelper.DB_NAME;
String backupDBPath =DBHelper.DB_NAME;
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd[1], backupDBPath);

if (currentDB.exists()) {
FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel dst = new FileOutputStream(backupDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
}
}
} catch (Throwable e) {
e.printStackTrace();
}
}






android sqlite






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 at 5:58









Mike Gorospe

14914




14914












  • Of course that apps private folder will get removed, if the app is deinstalled. That is the point of deinstalling it. You certainly can copy any files you like to the sdcard, but they will be visible and everyone can tamper with those files in the public space. You can use normal file handling functions as offered by android/java for that, this works just fine.
    – arkascha
    Nov 21 at 6:05


















  • Of course that apps private folder will get removed, if the app is deinstalled. That is the point of deinstalling it. You certainly can copy any files you like to the sdcard, but they will be visible and everyone can tamper with those files in the public space. You can use normal file handling functions as offered by android/java for that, this works just fine.
    – arkascha
    Nov 21 at 6:05
















Of course that apps private folder will get removed, if the app is deinstalled. That is the point of deinstalling it. You certainly can copy any files you like to the sdcard, but they will be visible and everyone can tamper with those files in the public space. You can use normal file handling functions as offered by android/java for that, this works just fine.
– arkascha
Nov 21 at 6:05




Of course that apps private folder will get removed, if the app is deinstalled. That is the point of deinstalling it. You certainly can copy any files you like to the sdcard, but they will be visible and everyone can tamper with those files in the public space. You can use normal file handling functions as offered by android/java for that, this works just fine.
– arkascha
Nov 21 at 6:05












2 Answers
2






active

oldest

votes


















0














getContext().getExternalFilesDirs(null) returns somewhere on the SD card but still belongs to the application.



You should use Environment.getExternalStorageDirectory().






share|improve this answer





















  • I have tried this but there is no back up created
    – Mike Gorospe
    Nov 21 at 6:19



















0














this should return the internal storage at [0] and the external storage at [1]:



File paths = ContextCompat.getExternalFilesDirs(getContext(), null);


alternatively ...



File sdcard = Environment.getExternalStorageDirectory();
String path = sdcard.getAbsolutePath();


the .getAbsolutePath() might be the trick here - for both methods of obtaining the handle.






share|improve this answer























  • I'm testing this now. thanks
    – Mike Gorospe
    Nov 21 at 6:22










  • as of now its not working. how can I create an public folder for it?
    – Mike Gorospe
    Dec 4 at 6:06











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53406059%2fcreating-a-sqlite-backup-on-root-of-sd-card%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














getContext().getExternalFilesDirs(null) returns somewhere on the SD card but still belongs to the application.



You should use Environment.getExternalStorageDirectory().






share|improve this answer





















  • I have tried this but there is no back up created
    – Mike Gorospe
    Nov 21 at 6:19
















0














getContext().getExternalFilesDirs(null) returns somewhere on the SD card but still belongs to the application.



You should use Environment.getExternalStorageDirectory().






share|improve this answer





















  • I have tried this but there is no back up created
    – Mike Gorospe
    Nov 21 at 6:19














0












0








0






getContext().getExternalFilesDirs(null) returns somewhere on the SD card but still belongs to the application.



You should use Environment.getExternalStorageDirectory().






share|improve this answer












getContext().getExternalFilesDirs(null) returns somewhere on the SD card but still belongs to the application.



You should use Environment.getExternalStorageDirectory().







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 at 6:05









AIMIN PAN

20628




20628












  • I have tried this but there is no back up created
    – Mike Gorospe
    Nov 21 at 6:19


















  • I have tried this but there is no back up created
    – Mike Gorospe
    Nov 21 at 6:19
















I have tried this but there is no back up created
– Mike Gorospe
Nov 21 at 6:19




I have tried this but there is no back up created
– Mike Gorospe
Nov 21 at 6:19













0














this should return the internal storage at [0] and the external storage at [1]:



File paths = ContextCompat.getExternalFilesDirs(getContext(), null);


alternatively ...



File sdcard = Environment.getExternalStorageDirectory();
String path = sdcard.getAbsolutePath();


the .getAbsolutePath() might be the trick here - for both methods of obtaining the handle.






share|improve this answer























  • I'm testing this now. thanks
    – Mike Gorospe
    Nov 21 at 6:22










  • as of now its not working. how can I create an public folder for it?
    – Mike Gorospe
    Dec 4 at 6:06
















0














this should return the internal storage at [0] and the external storage at [1]:



File paths = ContextCompat.getExternalFilesDirs(getContext(), null);


alternatively ...



File sdcard = Environment.getExternalStorageDirectory();
String path = sdcard.getAbsolutePath();


the .getAbsolutePath() might be the trick here - for both methods of obtaining the handle.






share|improve this answer























  • I'm testing this now. thanks
    – Mike Gorospe
    Nov 21 at 6:22










  • as of now its not working. how can I create an public folder for it?
    – Mike Gorospe
    Dec 4 at 6:06














0












0








0






this should return the internal storage at [0] and the external storage at [1]:



File paths = ContextCompat.getExternalFilesDirs(getContext(), null);


alternatively ...



File sdcard = Environment.getExternalStorageDirectory();
String path = sdcard.getAbsolutePath();


the .getAbsolutePath() might be the trick here - for both methods of obtaining the handle.






share|improve this answer














this should return the internal storage at [0] and the external storage at [1]:



File paths = ContextCompat.getExternalFilesDirs(getContext(), null);


alternatively ...



File sdcard = Environment.getExternalStorageDirectory();
String path = sdcard.getAbsolutePath();


the .getAbsolutePath() might be the trick here - for both methods of obtaining the handle.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 at 6:11

























answered Nov 21 at 6:06









Martin Zeitler

14.2k33863




14.2k33863












  • I'm testing this now. thanks
    – Mike Gorospe
    Nov 21 at 6:22










  • as of now its not working. how can I create an public folder for it?
    – Mike Gorospe
    Dec 4 at 6:06


















  • I'm testing this now. thanks
    – Mike Gorospe
    Nov 21 at 6:22










  • as of now its not working. how can I create an public folder for it?
    – Mike Gorospe
    Dec 4 at 6:06
















I'm testing this now. thanks
– Mike Gorospe
Nov 21 at 6:22




I'm testing this now. thanks
– Mike Gorospe
Nov 21 at 6:22












as of now its not working. how can I create an public folder for it?
– Mike Gorospe
Dec 4 at 6:06




as of now its not working. how can I create an public folder for it?
– Mike Gorospe
Dec 4 at 6:06


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53406059%2fcreating-a-sqlite-backup-on-root-of-sd-card%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Wiesbaden

Marschland

Dieringhausen