Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import android.content.Context;

import android.os.Environment;

import android.util.Log;
import android.widget.Toast;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import java.nio.channels.FileChannel;

public class Main {
    /**
     * Create a backup of the database.
     *
     * @param context who calls the function.
     * @param dbName  Name of the database to backup. (If empty, radiomap.db is used).
     */
    public static void exportDb(Context context, String dbName) {
        try {
            if (dbName.equals(""))
                dbName = "radiomap.db";

            File sd = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
            File data = Environment.getDataDirectory();

            if (sd.canWrite()) {
                String currentDBPath = "//data//" + context.getApplicationContext().getPackageName()
                        + "//databases//" + dbName;
                File currentDB = new File(data, currentDBPath);
                File backupDB = new File(sd, dbName);

                FileChannel src = new FileInputStream(currentDB).getChannel();
                FileChannel dst = new FileOutputStream(backupDB).getChannel();
                dst.transferFrom(src, 0, src.size());
                src.close();
                dst.close();

                Toast.makeText(context, "Datenbank nach Downloads exportiert!", Toast.LENGTH_LONG).show();
            }
        } catch (Exception e) {
            Log.e("Utils", e.getMessage());
            Toast.makeText(context, "Exportieren fehlgeschlagen!", Toast.LENGTH_LONG).show();
        }
    }
}