Save a csv file

1

I'm trying to save a csv file with data from table 2, so I did as follows:

Typing method:

public void escrever(int PedLinhas, int ProLinhas) {
    File caminho = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +
            "/exportar");

    if (!caminho.exists()) {
        caminho.mkdir();
    }

    File file = new File(caminho.getPath() + "/export.csv");

    try {
        FileOutputStream out = new FileOutputStream(file);

        String aux1 = "PED_MESA;PED_DATA;PED_TEMPO;PED_ATEND";
        String aux2 = "PPED_CODIGO;PPPRO_CODIGO;PPRO_QUANT;PPRO_UNITARIO;PPRO_ITEM";

        for (int i = 0; i < PedLinhas; i++) {
            out.write(aux1.getBytes(), 0, aux1.getBytes().length);
            for (int i2 = 0; i2 < ProLinhas; i++) {
                out.write(aux2.getBytes(), 0, aux2.getBytes().length);
            }
        }
        out.flush();
        out.close();

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

loads request:

public int carregaPedido() {
    Cursor cursor;
    int linhasPe;

    db = banco.getReadableDatabase();
    cursor = db.rawQuery("SELECT COUNT(_id) FROM PEDIDO", null);

    if (cursor != null){
        cursor.moveToFirst();
    }

    linhasPe = cursor.getColumnCount();

    return linhasPe;
}

method loadsProducts:

public int carregaProdutos() {
    Cursor cursor;
    int linhasPp;

    db = banco.getReadableDatabase();
    cursor = db.rawQuery("SELECT COUNT(*) FROM " + CriaBanco.getPproTabela(), null);

    if (cursor != null){
        cursor.moveToFirst();
    }

    linhasPp = cursor.getColumnCount();
    return linhasPp;
}

Activity export:

public class ExportaActivity extends AppCompatActivity{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.exporta_ftp);

        BancoController crud = new BancoController(getBaseContext());

        int linhasPe = crud.carregaPedido();
        int linhasPp = crud.carregaProdutos();



        crud.escrever(linhasPe, linhasPp);
    }
}

and is giving a permission error, but I put them in the manifest.

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="realsysten.com.br.sigarestaurante">

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".LoginActivity"/>
        <activity android:name=".OpcoesActivity"/>
        <activity android:name=".CadAtendenteActivity"/>
        <activity android:name=".AlterarSenhaActivity"/>
        <activity android:name=".LoginAtendActivity"/>
        <activity android:name=".AlterarAtendenteActivity"/>
        <activity android:name=".ListarMesaActivity"/>
        <activity android:name=".ListarProdutosActivity"/>
        <activity android:name=".RealizarPedidoActivity"/>
        <activity android:name=".ExportaActivity"/>
    </application>
</manifest>

but is giving the following error:

  

java.io.FileNotFoundException:   /storage/emulated/0/exportar/export.csv: open failed: EACCES   (Permission denied)

Does anyone know why?

--- Edit ---

I tried to do this:

public class ExportarPedidoTask extends AsyncTask<String, Void, Boolean> {

        private final ProgressDialog dialog = new ProgressDialog(RealizarPedidoActivity.this);

        @Override
        protected void onPreExecute() {
            this.dialog.setMessage("Transferindo o pedido...");
            this.dialog.show();
        }

        @Override
        protected Boolean doInBackground(final String... params) {

            File dbFile = getDatabasePath("Sigadm.db");
            System.out.println(dbFile);
            File exportDir = new File(Environment.getExternalStorageDirectory(), "");

            if (!exportDir.exists()) {
                exportDir.mkdir();
            }

            File file = new File(exportDir, "pedido.csv");
            try {
                file.createNewFile();
                CSVWriter csvWriter = new CSVWriter(new FileWriter(file));

                Cursor curCSVPed = db.rawQuery("SELECT * FROM " +
                        CriaBanco.getPedTabela() + " WHERE _id = " + pedItem, null);

                csvWriter.writeNext(curCSVPed.getColumnNames());

                Cursor curCSVPro = db.rawQuery("SELECT * FROM " +
                        CriaBanco.getPproTabela() + " WHERE PED_CODIGO = " + pedItem, null);

                csvWriter.writeNext(curCSVPro.getColumnNames());

                while (curCSVPed.moveToNext()) {

                    String arrStrPed[] = {curCSVPed.getString(1), curCSVPed.getString(2),
                            curCSVPed.getString(3), curCSVPed.getString(4)};

                    csvWriter.writeNext(arrStrPed);

                    while (curCSVPro.moveToNext()) {

                        String arrStrPro[] = {curCSVPro.getString(1), curCSVPro.getString(2),
                                curCSVPro.getString(3), curCSVPro.getString(4),
                                curCSVPro.getString(5)};

                        csvWriter.writeNext(arrStrPed);
                    }
                }

                csvWriter.close();
                curCSVPed.close();
                curCSVPro.close();

                return true;

            } catch (IOException e) {
                Log.e("Erro file", e.getMessage(), e);
                return false;
            } catch (SQLException sqlEx) {
                Log.e("Erro sql", sqlEx.getMessage(), sqlEx);
                return false;
            }
        }

        protected void onPostExecute(final Boolean sucess){
            if(this.dialog.isShowing()){
                this.dialog.dismiss();
            }

            if (sucess){
                Toast.makeText(RealizarPedidoActivity.this, "Arquivo transferido com sucesso.",
                        Toast.LENGTH_LONG).show();
            }else {
                Toast.makeText(RealizarPedidoActivity.this, "Erro ao transferir arquivo.",
                        Toast.LENGTH_LONG).show();
            }
        }
    }

and it's the same error.

    
asked by anonymous 10.06.2016 / 14:14

0 answers