Hello ... sorry if I posted in the wrong place or wrongly, because I am new to the forum (although I always read it).
Lollipop helped me a lot with the explanation: link
But I have 3 doubts:
If I just want to create the file without content, I only need remove the lines of code:
...
String content = "Olá todos?"; //retirar
...
outputStream.write(content.getBytes()); //retirar
...
I can create the directory and files in white (no content) in the
Edited below!
Please, I'm locked in that part of someone ... help me!
MainActivity.java
package br.com.aime.aimeappauditoria;
import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import static android.os.Environment.getDataDirectory;
import static br.com.aime.aimeappauditoria.R.id.btNovoBalanco;
import static br.com.aime.aimeappauditoria.R.id.nomeAuditor;
import static br.com.aime.aimeappauditoria.R.id.nomeFilial;
import static br.com.aime.aimeappauditoria.R.string.local;
public class MainActivity extends AppCompatActivity {
@SuppressLint("SimpleDateFormat")
public final SimpleDateFormat data = new SimpleDateFormat("dd/MM/yyyy");
public final Calendar c = Calendar.getInstance();
public EditText editTextBuscar;
public TextView textViewBipados;
public TextView textViewUltimosItens;
public TextView textViewLocal;
public TextView textViewAuditor;
public TextView textViewTotal;
public TextView textViewData;
public Button btnRecomecar;
public Button btnNovoBalanco;
public Button btnFinalizar;
String fileData = "data.txt";
// String fileAuditor = "auditor.txt";
// String fileBalanco = "balanco.txt";
// String fileLocal = "local.txt";
View popup1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textViewData = (TextView) findViewById(R.id.textViewData);
textViewTotal = (TextView) findViewById(R.id.textViewTotal);
textViewAuditor = (TextView) findViewById(R.id.textViewAuditor);
textViewLocal = (TextView) findViewById(R.id.textViewLocal);
editTextBuscar = (EditText) findViewById(R.id.editTextBuscar);
textViewUltimosItens = (TextView) findViewById(R.id.textViewUltimosItens);
textViewBipados = (TextView) findViewById(R.id.textViewBipados);
btnNovoBalanco = (Button) findViewById(btNovoBalanco);
btnFinalizar = (Button) findViewById(R.id.btnFinalizar);
btnRecomecar = (Button) findViewById(R.id.btnRecomecar);
btnNovoBalanco.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openPopupStart(v);
lerData();
}
});
btnRecomecar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
lerData();
}
});
}
public void lerData() {
@SuppressWarnings("MismatchedQueryAndUpdateOfCollection")
ArrayList<String> dataTXT;
String dataConteudo = "";
try {
FileReader dataOut2 = new FileReader((fileData));
BufferedReader dataLer = new BufferedReader(dataOut2);
dataTXT = new ArrayList<>();
while (dataLer.readLine() != null) {
dataTXT.add(dataConteudo);
textViewData.setText(dataConteudo);
dataOut2.close();
}
}catch( Exception e){
e.printStackTrace();
}
}
public void criarDataTXT(){
String data = c.getTime().toString();
//String auditor = textViewAuditor.getText().toString();
// String local = textViewLocal.getText().toString();
// String balanco = "";
File path = new File(getDataDirectory().getAbsolutePath(), "files");
File fileTXT = new File(path,fileData);
try {
FileWriter dataOut = new FileWriter(fileTXT);
BufferedWriter dataPrint = new BufferedWriter(dataOut);
dataPrint.write(data);
dataPrint.flush();
dataOut.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void criarBalanco(){
criarDataTXT();
}
public void openPopupStart(final View start) {
this.popup1 = start;
LayoutInflater layoutInflater = LayoutInflater.from(this);
@SuppressLint("InflateParams") View promptView = layoutInflater.inflate(R.layout.popup_start, null);
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Iniciar processo de balanço");
alert.setView(promptView);
final EditText input = (EditText) promptView.findViewById( nomeFilial);
final EditText inputAuditor = (EditText) promptView.findViewById( nomeAuditor);
input.requestFocus();
input.setHint( local);
input.setTextColor(Color.BLACK);
alert.setPositiveButton( R.string.OK, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String nomeFilial = input.getText().toString();
String nomeAuditor = inputAuditor.getText().toString();
// textViewLocal.setText( nomeFilial );
// textViewData.setText( data.format( c.getTime() ) );
// textViewAuditor.setText( nomeAuditor );
try {
if (nomeFilial.equals( "" ) || nomeAuditor.equals( "" )) {
input.setError( getString( R.string.localVazio ) );
inputAuditor.setError( getString( R.string.auditorVazio ) );
Toast.makeText( getApplicationContext(),
"Digite o Local e Auditor", Toast.LENGTH_SHORT ).show();
openPopupStart( null );
} else {
criarBalanco();
Toast.makeText( getApplicationContext(),
"Novo Balanço Criado!!!", Toast.LENGTH_SHORT ).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
} ).setNegativeButton( "Cancelar",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
Toast.makeText( getApplicationContext(),
"Novo Balanço cancelado!!!", Toast.LENGTH_SHORT ).show();
}
} );
// create an alert dialog
AlertDialog popup = alert.create();
popup.show();
}
}