I was able to import csv into my cell phone, but when I try to read the file so that the application inserts the information inside the csv, an error occurs:
java.io.FileNotFoundException: table.csv: open failed: ENOENT (No such file or directory)
Probably because the file is not saved in the directory that the code is looking for, let's go to the codes:
package realsysten.com.br.sigarestaurante;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
/**
* Created by Vitor on 16/06/2016.
*/
public class ImportarBancoActivity extends AppCompatActivity {
Spinner spTabela;
ArrayList<String> arquivosCelular = new ArrayList<String>();
//ProgressDialog dialog;
int op;
BancoController crud;
FTPController ftp;
String nome;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.importar_banco);
spTabela = (Spinner) findViewById(R.id.spTabela);
chamaTabela();
Button b3 = (Button) findViewById(R.id.btnInserir);
Button b4 = (Button) findViewById(R.id.btnTabCancelar);
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
nome = spTabela.getSelectedItem().toString();
crud = new BancoController(getBaseContext());
ftp = new FTPController();
if (nome.equals("mesa.csv")) {
op = 1;
} else if (nome.equals("produto.csv")) {
op = 2;
} else {
op = 3;
}
try {
FileReader arq = new FileReader(nome);
BufferedReader lerArq = new BufferedReader(arq);
String linha = lerArq.readLine();
switch (op) {
case 1:
while (linha != null) {
String[] dados = linha.split(";");
String codigo = dados[0];
String descricao = dados[1];
String unitario = dados[2];
crud.insereProdutos(codigo, descricao, unitario);
linha = lerArq.readLine();
}
break;
case 2:
while (linha != null) {
String[] dados = linha.split(";");
String codigo = dados[0];
String descricao = dados[1];
String situacao = dados[2];
crud.insereControle(codigo, descricao, situacao);
linha = lerArq.readLine();
}
break;
case 3:
Toast.makeText(getApplicationContext(), "O nome do arquivo texto" +
"não esta escrito da forma certa", Toast.LENGTH_LONG).show();
break;
}
arq.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
b4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(ImportarBancoActivity.this, OpcoesActivity.class);
startActivity(i);
finish();
}
});
}
private void chamaTabela() {
new Thread(new Runnable() {
@Override
public void run() {
listarTabela();
}
}).start();
}
private void listarTabela() {
File diretorio = new File(Environment.getExternalStorageDirectory() + "/Import");
File[] arquivos = diretorio.listFiles();
if (arquivos != null) {
int lenght = arquivos.length;
for (int i = 0; i < lenght; ++i) {
File f = arquivos[i];
if (f.isFile()) {
arquivosCelular.add(f.getName());
}
}
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, arquivosCelular);
runOnUiThread(new Runnable() {
@Override
public void run() {
spTabela.setAdapter(arrayAdapter);
}
});
}
}
}
The error occore here: FileReader arq = new FileReader(nome);
I think this command is searching the wrong place for the file table.csv, because this file is saved in my SD. is there any way to make the application search on: Environment.getExternalStorageDirectory()
?
thank you from ja