Pergunta

i developed application for sales force in our company.most of them do sales in rural areas and more times no signal for those areas.then they tell us they synch their sales to server.but more sales details are lost. then i try to develop tool as third party tool for my app to save local db file to phone internal memory.then we can get it via email.here is code

    try {
        File sd = Environment.getExternalStorageDirectory();
        File data = Environment.getDataDirectory();
        Toast.makeText(getApplicationContext(), "SD Card Detecting ", Toast.LENGTH_SHORT).show();
        if (sd.canWrite()) {
            Toast.makeText(getApplicationContext(), "Backup is writing to SD card", Toast.LENGTH_SHORT).show();
            String currentDBPath = "//data//com.lk.lankabell.android.activity//databases//TSRDBNEW";
            String backupDBPath = "TSRDBNEW-Backup";
            File currentDB = new File(data, currentDBPath);
            File backupDB = new File(sd, 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();
                Toast.makeText(getApplicationContext(), "Backup is successful to SD card", Toast.LENGTH_SHORT).show();
            }
        }
    } catch (Exception e) {
        Toast.makeText(getApplicationContext(), "Backup is un-success to SD card", Toast.LENGTH_SHORT).show();
    }

i used my main app [android+ksoap+axis2] technologies.

please advice me to implement this.i tried with above code.but it doesn't work.its need sd card too.any issue with my db url.?

enter image description here

Foi útil?

Solução

You cannot "get" a file from data "private" folder of any app so easily. The best way is, as you do creating/updateing/deleting from your classes to the DB, create a new query that gets the information you want, save it as String, and then save in sdcard in a txt file, or json file, or some format that you want. It could be a possible solution (without DB model can't do more) using the CSV format:

public void generateTXTOnSD(String[] lines_txt) 
{
    // lines_txt contains an array of each register in CSV format.
    try
    {
        File root = new File(Environment.getExternalStorageDirectory(),"NameOfDirectoryYouWantToSave");
        if (!root.exists()) 
        {
            root.mkdirs();
        }
        Calendar cal = Calendar.getInstance();
        // I made this to no override early versions of data I have stored.
        String hour= String.valueOf(cal.get(cal.HOUR_OF_DAY))+"_"+String.valueOf(cal.get(cal.MINUTE))+"_"+String.valueOf(cal.get(cal.SECOND));
        File txtfile = new File(root, name_txt+hour+".txt");
        FileWriter writer = new FileWriter(txtfile);
        //Every line contain the info you want to save
        for (String line_txt : lines_txt) {
            writer.append(line_txt);
            writer.append('\r');
            writer.append('\n');

        }
        writer.flush();
        writer.close();


    }
    catch(IOException e)
    {
        // ERROR

    }
} 

Hope this helps.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top