How to delete a SD Card file?

Asked

Viewed 39 times

2

I have an application that saves information from a requested table within Sqlite in a CSV file and then that file is transferred to an FTP.

I need this file, stored in my SD card, to be deleted as soon as it is exported to FTP, how do I do this?

Code:

Export button -

Button bre = (Button) findViewById(R.id.btnExportar);
        bre.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                data = new SimpleDateFormat("dd." + "MM" + ".yyyy").format(new Date());
                hora = new SimpleDateFormat("HH.mm.SS").format(new Date());

                final BancoController crud = new BancoController(getBaseContext());

                crud.escrever(data, hora);

                crud.deletaExportar();


                final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("Atenção");
                builder.setMessage("Desaja exportar todos os pedidos realizados?");
                builder.setPositiveButton("Sim", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Cursor svCursor = crud.verificaServidor(1);

                        dialog = ProgressDialog.show(MainActivity.this, "Aguarde...", "Sincronizando dados...",
                                false, true);

                        servidor = svCursor.getString(0);
                        login = svCursor.getString(1);
                        senha = svCursor.getString(2);
                        exportarPedido();
                    }
                });

                builder.setNegativeButton("Não", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getApplicationContext(), "Exportação cancelada...",
                                Toast.LENGTH_LONG).show();
                    }
                });

                alerta = builder.create();
                alerta.show();
            }
        });

Method of writing -

public void escrever(String Data, String Hora) {

        File caminho = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +
                "/exportar");

        if (!caminho.exists()) {
            caminho.mkdir();
        }
        int PedLinhas = carregaPedido();


        File file = new File(caminho.getPath() + "/export_" + Data + "_" + Hora + ".csv");

        try {
            FileOutputStream out = new FileOutputStream(file);

            String espaco = "\r\n";

            for (int i = 1; i <= PedLinhas; i++) {

                Cursor cs  = pegarIdPedido();

                String pedId = cs.getString(cs.getColumnIndexOrThrow(CriaBanco.getPedId()));

                Cursor cursor = pegaPedido(Integer.parseInt(pedId));

                int linha = 1;

                String id = cursor.getString(0);
                String cliente = cursor.getString(2);
                String pagamento = cursor.getString(3);
                String carteira = cursor.getString(4);
                String data = cursor.getString(5);
                String emissao = cursor.getString(6);
                String obs = cursor.getString(7);

                String aux1 = linha + ";" + cliente + ";" + pagamento + ";" + carteira + ";" + data + ";" +
                        emissao + ";" + obs + ";\r\n";

                out.write(aux1.getBytes(), 0, aux1.getBytes().length);

                int ProLinhas = carregaProdutos(Integer.parseInt(id));

                for (int i2 = 0; i2 < ProLinhas; i2++) {

                    Cursor cursor1 = pegaPedProd(Integer.parseInt(id));
                    String pedido = cursor1.getString(1);
                    String produto = cursor1.getString(2);
                    String quantidade = cursor1.getString(3);
                    String unitario = cursor1.getString(4);
                    String item = cursor1.getString(5);

                    String aux2 = pedido + ";" + produto + ";" + quantidade + ";" + unitario + ";" + item + ";\r\n";


                    out.write(aux2.getBytes(), 0, aux2.getBytes().length);
                }

                out.write(espaco.getBytes(), 0, espaco.getBytes().length);
                deletaPedido(Integer.parseInt(pedId));
                linha++;
            }

            deletaExportar();
            out.flush();
            out.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

method exported -

private void efetuarUpload() {
    try {
        FTPController ftp = new FTPController();
        ftp.conectar(servidor, login, senha, 21);
        ftp.mudarDiretorio("/Export");
        ftp.upload("/exportar/export_" + data + "_" + hora + ".csv", "/export_" + data + "_" + hora + ".csv");
    } catch (Exception e) {
        e.getStackTrace();
    }
}

Upload method -

public boolean upload(String diretorio, String nomeArquivo) {
        boolean status = false;
        try {
            FileInputStream arqEnviar = new FileInputStream(Environment.getExternalStorageDirectory() + diretorio);
            mFTP.setFileTransferMode(FTPClient.STREAM_TRANSFER_MODE);
            mFTP.setFileType(FTPClient.STREAM_TRANSFER_MODE);
            mFTP.storeFile(nomeArquivo, arqEnviar);
            desconecta();
            return status;
        } catch (Exception e) {
            Log.e(TAG, "Erro: falha ao efetuar upload. " + e.getMessage());
        }
        return status;
    }

1 answer

3


Save the file path after saving and when you want to delete it use:

File file = new File(caminhoDoArquivo);
file.delete();
  • 1

    worked very thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.