Android - Problema para guardar en la SD

 
Vista:
sin imagen de perfil

Problema para guardar en la SD

Publicado por alberto (1 intervención) el 14/01/2017 17:13:11
Tengo un problema para guardar un archivo de texto en la SD. Este es el codigo al pulsar el boton:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public void guardar(View v){
 
    String nomArchivo=titulo.getText().toString();
 
    try{
            File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), nomArchivo);
 
            File f = new File(file, "prueba_sd.txt");
            OutputStreamWriter fout = new OutputStreamWriter(new FileOutputStream(f,true));
            fout.write("hola");
            fout.close();
 
    }
    catch(IOException e){
       Toast.makeText(this,"No se pudo grabar",Toast.LENGTH_SHORT),show;
 
    }
}

La variable nomArchivo viene de un edittext, por lo que no da problema, el problema es que siempre me tira de la exception y me salta el toast siempre que pulso guardar.

El manifest lo tengo asi:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.albertomarley.almacenamientosd">
 
    <uses-permission
        android:name="android.permission.WRITE_EXTERNAL_STORAGE"
        android:maxSdkVersion="18" />
 
    <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>
    </application>
 
</manifest>

y cuando hago prueba de permisos me pone que esta montada la tarjeta sd, asique sigo sin entender porque me salta todo el rato la excepcion, trabajo con la ultima version del android studio, si alguien me pudiera ayudar estaria muy agradecido
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
1
Responder