Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static boolean importDB(WelcomeActivity activity) {
- try {
- File bcpOldDb = new File(Environment.getExternalStorageDirectory()+File.separator+"old.db");
- File newDb = new File(Environment.getExternalStorageDirectory()+File.separator+"backup.db");
- File oldDb = new File("data/data/" + context.getPackageName() + "/databases/" + databaseHelper.getDatabaseName());
- if (newDb.exists()) {
- copy(oldDb, bcpOldDb);
- copy(newDb, oldDb);
- activity.finish();
- newDb.delete();
- Intent it = new Intent(activity, activity.getClass());
- activity.startActivity(it);
- activity.finish();
- }else{
- Toast.makeText(activity, "Arquivo Backup.db nao existe", Toast.LENGTH_LONG).show();
- }
- return true;
- } catch (Exception e) {
- App.log(e.toString());
- }
- return false;
- }
- public static void exportDB(final Context context, String sendTo, String title, String body) {
- App.log("exportDB", "EXPORT");
- try {
- File dbFile = new File("data/data/" + context.getPackageName() + "/databases/" + databaseHelper.getDatabaseName());
- Log.d("EXPORT", dbFile.toString());
- FileInputStream fis = new FileInputStream(dbFile);
- String outFileName = Environment.getExternalStorageDirectory()+File.separator+"backup.db";
- OutputStream output = new FileOutputStream(outFileName);
- byte[] buffer = new byte[1024];
- int length;
- while ((length = fis.read(buffer)) > 0) {
- output.write(buffer, 0, length);
- }
- output.flush();
- output.close();
- fis.close();
- Toast.makeText(context, "Backup concluido", Toast.LENGTH_LONG).show();
- final OkHttpClient okHttpClient = new OkHttpClient.Builder()
- .writeTimeout(1500000, TimeUnit.SECONDS)
- .readTimeout(150000, TimeUnit.SECONDS)
- .build();
- RequestBody requestBody = new MultipartBody.Builder()
- .setType(MultipartBody.FORM)
- .addFormDataPart("sendTo", sendTo)
- .addFormDataPart("body", body)
- .addFormDataPart("title", title)
- .addFormDataPart("db", "db",
- RequestBody.create(MediaType.parse("text/csv"), dbFile)).build();
- final Request request = new Request.Builder()
- .url("http://www.allingressos.com.br/rest/banco.php")
- .post(requestBody)
- .build();
- new Thread(){
- @Override
- public void run() {
- try {
- Response response = okHttpClient.newCall(request).execute();
- Log.d("EXPORT", response.body().string());
- ((Activity)context).runOnUiThread(new Runnable() {
- @Override
- public void run() {
- Toast.makeText(context, "Banco enviado", Toast.LENGTH_LONG).show();
- }
- });
- }catch (Exception e){
- e.printStackTrace();
- Log.d("EXPORT", e.getMessage());
- }
- }
- }.start();
- } catch (Exception e) {
- App.log(e.getLocalizedMessage(), "EXPORT");
- Log.d("EXPORT", e.getMessage());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement