• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Issue to write SD Card

Good morning,

I would appreciate some help with an issue that I have with a code in Java with Android Studio. This work perfectly in Eclipse, but not with android.

This apk consists of saving data from a form to a txt file.

I can't see any error while debugging, but no folder or file is created by this apk.

These are the permissions given to the apk
```
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
```
In the main activity, I put the requests to those permissions

```
int permissionCheck = ContextCompat.checkSelfPermission( this, Manifest.permission.WRITE_EXTERNAL_STORAGE);

if (permissionCheck != PackageManager.PERMISSION_GRANTED) {

Log.i("Message", "Don't have permission to write in SD card.");

ActivityCompat.requestPermissions(this, new String[ {

Manifest.permission.WRITE_EXTERNAL_STORAGE}, 225);

} else {

Log.i("Message", "You have access!"); }
```


This is the OnCreate method which works fine:

``` private String nombreArchivo = "control.txt";
private String nombreCarpeta = "OfflineGestion";

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_control);

File tarjeta_sd = Environment.getExternalStorageDirectory();
f_carpeta = new File(tarjeta_sd.getAbsolutePath(),nombreCarpeta);
//f = new File()
if (!f_carpeta.exists()) {
f_carpeta.mkdirs();
}

String pathArchivo = Environment.getExternalStorageDirectory() + File.separator + nombreCarpeta + File.separator + nombreArchivo;
//Si el archivo no existe, crear archivo con contenido básico


f_archivo = new File(pathArchivo);
if (!f_archivo.exists()) {
crearArchivo(f_archivo);
}

sptitulo = (Spinner) findViewById(R.id.sptitulo);
//Creating the ArrayAdapter instance having the list
ArrayAdapter aa = new ArrayAdapter(this,android.R.layout.simple_spinner_item,tituloSpinner);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//Setting the ArrayAdapter data on the Spinner
sptitulo.setAdapter(aa);


edEntidad = (EditText) findViewById(R.id.entidad);
edCantidad = findViewById(R.id.cantidad);
initDatePicker();
dateButton = findViewById(R.id.fecha);
dateButton.setText(getTodayDate());
edDescripcion = findViewById(R.id.edDescripcion);

rbGasto = findViewById(R.id.rbGasto);
rbIngreso = findViewById(R.id.rbIngreso);
chExtra = findViewById(R.id.chExtra);
chExtraEspecial = findViewById(R.id.chExtraEspecial);


}```

This is the method to create a file. When debugging and the pointer arrives to this sentence fr = new FileWriter(fileSt);
it moves to IOException
```
Toast.makeText(this,"No se pudo guardar",Toast.LENGTH_SHORT).show();
e.printStackTrace();

```

```
private void crearArchivo(File f) {

FileWriter fr = null;
PrintWriter pw = null;
String fileSt = f.toString();
try {

fr = new FileWriter(fileSt);
pw = new PrintWriter(fr);

pw.println("insert into movimientos ('titulo','entidad','cantidad','fecha','operacion','descripcion') values");


//fr.flush();

Toast.makeText(this,"Guardado correctamente",Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(this,"No se pudo guardar",Toast.LENGTH_SHORT).show();
e.printStackTrace();
} finally {
try {
if (null != fr) {
fr.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}

}
}


```
https://stackoverflow.com/questions/ask
 
Back
Top Bottom