IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Android Discussion :

Stockage fichier texte impossible


Sujet :

Android

  1. #1
    Membre régulier
    Homme Profil pro
    Software Engineer
    Inscrit en
    Février 2013
    Messages
    139
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Software Engineer
    Secteur : High Tech - Électronique et micro-électronique

    Informations forums :
    Inscription : Février 2013
    Messages : 139
    Points : 94
    Points
    94
    Par défaut Stockage fichier texte impossible
    Bonjour,

    J'essaie en vain de stocker un fichier texte sur ma tablette, mais rien ne se fait, cela fait plusieur semaine que je ne trouve aucune réponse sur les forum/tuto, pourtant j'ai essayé plein de méthodes différentes ...
    En gros mon activity se compose d'un champs titre de la note, le champs de texte principal, un bouton pour enregistrer et un bouton pour vider le champs de texte.

    Voila le code :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    package com.example.hfzd;
     
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStreamWriter;
    import java.text.SimpleDateFormat;
    import java.util.Date;
     
    import android.app.Activity;
    import android.content.Context;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
     
     
    public class NoteActivity extends Activity{
     
    	    /** Called when the activity is first created. */
    	    @Override
    	    public void onCreate(Bundle savedInstanceState) {
    	    	super.onCreate(savedInstanceState);
     
    	    	setContentView(R.layout.activity_note);
     
    	    	final EditText text = (EditText) findViewById(R.id.texte_note);
                Button valider = (Button) findViewById(R.id.save);
                Button vider = (Button) findViewById(R.id.vide);
                TextView letitre = (TextView) findViewById(R.id.titre_note);
                final String titre = letitre.getText().toString();
     
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
                final String dateStr = simpleDateFormat.format(new Date()); 
     
     
                //////////////////////SAUVEGARDER FICHIER//////////////////////
     
                valider.setOnClickListener(new OnClickListener() {                
                       public void onClick(View v) {                        
     
                              ecrireFichier(titre+"_"+dateStr+".txt", text.getText().toString());
     
                       }
     
                });
     
                //////////////////////VIDER CHAMP DE TEXTE//////////////////////
                vider.setOnClickListener(new OnClickListener() {                  
     
                       public void onClick(View v) {                        
     
                              text.setText("");
     
                       }
     
                });	 
     
    	    }
     
     
     
    	    //////////////////////FONCTION ECRITURE FICHIER//////////////////////
    	    public void ecrireFichier(String nomFichier,String monText) {
     
                BufferedWriter writer = null;
     
                try {
                	String FILENAME = nomFichier;
                	String string = monText;
     
                	FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_WORLD_READABLE);
                	fos.write(string.getBytes());
                	fos.close();
                } catch (Exception e) {
                    Log.e("NoteActivity","Failed to write file",e);
                } finally {
                    if (writer != null) {
                        try {
                             writer.close();
                        } catch (IOException e) {
                             Log.e("NoteActivity","Failed to gracefully close output stream",e);
                        }
                    }
                }
    	    }
     
     
    }
    Merci d'avance, j'ai vraiment besoin de votre aide.

  2. #2
    Membre régulier
    Homme Profil pro
    Software Engineer
    Inscrit en
    Février 2013
    Messages
    139
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Software Engineer
    Secteur : High Tech - Électronique et micro-électronique

    Informations forums :
    Inscription : Février 2013
    Messages : 139
    Points : 94
    Points
    94
    Par défaut
    Bon j'ai quand même testé ce code :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    package com.example.hfzd;
     
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.text.SimpleDateFormat;
    import java.util.Date;
     
    import android.app.Activity;
    import android.content.Context;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    import android.widget.Toast;
     
     
    public class NoteActivity extends Activity{
     
    	    /** Called when the activity is first created. 
                 * @return */
    	    @Override
    	    public void onCreate(Bundle savedInstanceState) {
    	    	super.onCreate(savedInstanceState);
     
    	    	setContentView(R.layout.activity_note);
     
    	    	final EditText text = (EditText) findViewById(R.id.texte_note);
                Button valider = (Button) findViewById(R.id.save);
                Button vider = (Button) findViewById(R.id.vide);
                TextView letitre = (TextView) findViewById(R.id.titre_note);
                final String titre = letitre.getText().toString();
     
                /*
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
                final String dateStr = simpleDateFormat.format(new Date()); 
                */
     
     
     
                setContentView(R.layout.activity_note);
                Button btvoir = (Button) findViewById(R.id.vide);
                Button btecrire = (Button) findViewById(R.id.save);
                btvoir.setOnClickListener(new Button.OnClickListener() {
                   public void onClick(View v) {
     
                	   		Context lecontext = getBaseContext();
                             Read(lecontext);
                    } 
                    });  
     
               btecrire.setOnClickListener(new Button.OnClickListener() {
                        public void onClick(View v) {
                         TextView datatext = (TextView) findViewById(R.id.texte_note);
                        String sQuantite = datatext.getText()+"\n";
                        Context lecontext = getBaseContext();
                        Write(lecontext,sQuantite);
                  } 
                 });
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
                /*
                //////////////////////SAUVEGARDER FICHIER//////////////////////
     
                valider.setOnClickListener(new OnClickListener() {                
                       public void onClick(View v) {                        
     
                              ecrireFichier(titre+"_"+dateStr+".txt", text.getText().toString());
     
                       }
     
                });
     
                //////////////////////VIDER CHAMP DE TEXTE//////////////////////
                vider.setOnClickListener(new OnClickListener() {                  
     
                       public void onClick(View v) {                        
     
                              text.setText("");
     
                       }
     
                });	 
                
    	    }
     
     
     
    	    //////////////////////FONCTION ECRITURE FICHIER//////////////////////
    	    public void ecrireFichier(String nomFichier,String monText) {
     
                BufferedWriter writer = null;
     
                try {
                	String FILENAME = nomFichier;
                	String string = monText;
     
                	FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_WORLD_READABLE);
                	fos.write(string.getBytes());
                	fos.close();
                } catch (Exception e) {
                    Log.e("NoteActivity","Failed to write file",e);
                } finally {
                    if (writer != null) {
                        try {
                             writer.close();
                        } catch (IOException e) {
                             Log.e("NoteActivity","Failed to gracefully close output stream",e);
                        }
                    }
                }
    	    }
     
    */
     
    }
     
    	    public void Write(Context context, String data){ 
                FileOutputStream fOut = null; 
                OutputStreamWriter osw = null; 
     
                try{ 
                   fOut = context.openFileOutput("settings.dat",MODE_APPEND);       
                    osw = new OutputStreamWriter(fOut); 
                    osw.write(data); 
                    osw.flush(); 
                   //popup surgissant pour le résultat
                    Toast.makeText(context, "Settings saved",Toast.LENGTH_SHORT).show(); 
                    } 
                    catch (Exception e) {       
                            Toast.makeText(context, "Settings not saved",Toast.LENGTH_SHORT).show(); 
                    } 
                    finally { 
                       try { 
                              osw.close(); 
                              fOut.close(); 
                              } catch (IOException e) { 
                                       Toast.makeText(context, "Settings not saved",Toast.LENGTH_SHORT).show(); 
                              } 
                    } 
               }
     
     
     
            public String Read(Context context){ 
                FileInputStream fIn = null; 
                InputStreamReader isr = null; 
     
                char[] inputBuffer = new char[255]; 
                String data = null; 
     
                try{ 
                 fIn = context.openFileInput("settings.dat");       
                    isr = new InputStreamReader(fIn); 
                    isr.read(inputBuffer); 
                    data = new String(inputBuffer); 
                   //affiche le contenu de mon fichier dans un popup surgissant
                    Toast.makeText(context, " "+data,Toast.LENGTH_SHORT).show(); 
                    } 
                    catch (Exception e) {       
                              Toast.makeText(context, "Settings not read",Toast.LENGTH_SHORT).show(); 
                    } 
                    /*finally { 
                       try { 
                              isr.close(); 
                              fIn.close(); 
                              } catch (IOException e) { 
                                Toast.makeText(context, "Settings not read",Toast.LENGTH_SHORT).show(); 
                              } 
                    } */
                    return data; 
               }
    }
    Cela semble fonctionner mais impossible d'accéder au fichier dans le navigateur android, d'où cela peut-il venir ??

  3. #3
    Expert éminent

    Homme Profil pro
    Ingénieur systèmes et réseaux
    Inscrit en
    Février 2007
    Messages
    4 253
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Ingénieur systèmes et réseaux
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Février 2007
    Messages : 4 253
    Points : 7 618
    Points
    7 618
    Billets dans le blog
    3
    Par défaut
    Je ne comprends pas le problème....

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    openFileOutput(FILENAME, Context.MODE_WORLD_READABLE);
    Avez vous lu la documentation ?
    Open a private file associated with this Context's application package for writing. Creates the file if it doesn't already exist.
    Donc il n'est accessible qu'à l'application... on est bien d'accord ?
    N'oubliez pas de cliquer sur mais aussi sur si un commentaire vous a été utile !
    Et surtout

  4. #4
    Membre régulier
    Homme Profil pro
    Software Engineer
    Inscrit en
    Février 2013
    Messages
    139
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Software Engineer
    Secteur : High Tech - Électronique et micro-électronique

    Informations forums :
    Inscription : Février 2013
    Messages : 139
    Points : 94
    Points
    94
    Par défaut
    Ha oui en effet ..., donc comment je peu faire pour le rendre accessible pour tous et surtout pour pouvoir récupérer le fichier plus tard dans l'exporer ?

    Merci

  5. #5
    Expert éminent

    Homme Profil pro
    Ingénieur systèmes et réseaux
    Inscrit en
    Février 2007
    Messages
    4 253
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Ingénieur systèmes et réseaux
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Février 2007
    Messages : 4 253
    Points : 7 618
    Points
    7 618
    Billets dans le blog
    3
    Par défaut
    Là encore... tout est expliqué dans la documentation sur les external-storage
    N'oubliez pas de cliquer sur mais aussi sur si un commentaire vous a été utile !
    Et surtout

  6. #6
    Membre régulier
    Homme Profil pro
    Software Engineer
    Inscrit en
    Février 2013
    Messages
    139
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Software Engineer
    Secteur : High Tech - Électronique et micro-électronique

    Informations forums :
    Inscription : Février 2013
    Messages : 139
    Points : 94
    Points
    94
    Par défaut
    Ok merci mais il est question de stockage externe si j'ai bien compris ? Je souhaiterais stockers dans la mémoire interne ( remplacer external par internal ?)
    J'ai l'impression de tourner en rond ...

  7. #7
    Expert éminent

    Homme Profil pro
    Ingénieur systèmes et réseaux
    Inscrit en
    Février 2007
    Messages
    4 253
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Ingénieur systèmes et réseaux
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Février 2007
    Messages : 4 253
    Points : 7 618
    Points
    7 618
    Billets dans le blog
    3
    Par défaut
    external_storage = storage accessible depuis l’extérieur... cela ne veut pas dire sur un carte SD indépendante....
    N'oubliez pas de cliquer sur mais aussi sur si un commentaire vous a été utile !
    Et surtout

  8. #8
    Membre régulier
    Homme Profil pro
    Software Engineer
    Inscrit en
    Février 2013
    Messages
    139
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Software Engineer
    Secteur : High Tech - Électronique et micro-électronique

    Informations forums :
    Inscription : Février 2013
    Messages : 139
    Points : 94
    Points
    94
    Par défaut
    Ok d'accord merci je test ca et je vous tiens au courant en postant le code si cela fonctionne afin que ca puisse resservir a quelqu'un un jour.

  9. #9
    Membre régulier
    Homme Profil pro
    Software Engineer
    Inscrit en
    Février 2013
    Messages
    139
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Software Engineer
    Secteur : High Tech - Électronique et micro-électronique

    Informations forums :
    Inscription : Février 2013
    Messages : 139
    Points : 94
    Points
    94
    Par défaut
    Bonjour,

    Pour faire suite, j'ai lu la documentation et particulièrement sur getExternalStoragePublicDirectory et cela reste extrêmement flou pour moi, surtout l'exemple donné pour le stockage d'une image.

    Quelqu'un pourrait-il me donner un squelette de code pour stocker un fichier texte lisible depuis le navigateur de fichier ? ou sinon me donner plus d'indication sur le sujet ?

    Merci grandement.

  10. #10
    Membre actif
    Inscrit en
    Décembre 2008
    Messages
    280
    Détails du profil
    Informations forums :
    Inscription : Décembre 2008
    Messages : 280
    Points : 261
    Points
    261
    Par défaut
    Salut,

    Je ferai un tuto sur le stockage de fichier dans le week end sur mon site. Je te préviendrai ici quand il sera en ligne.
    Paye Tes Dettes - Applciation android.

    DevHackSecure - Pense bête d'un étudiant en informatique - Tutos DEV

    " I also realize that _everybody_ thinks that they are right, and that they are supported by all other right-thinking people. That's just how people work. We all think we're better than average." Linus Torvalds

  11. #11
    Membre régulier
    Homme Profil pro
    Software Engineer
    Inscrit en
    Février 2013
    Messages
    139
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Software Engineer
    Secteur : High Tech - Électronique et micro-électronique

    Informations forums :
    Inscription : Février 2013
    Messages : 139
    Points : 94
    Points
    94
    Par défaut
    Merci c'est sympa j'espère que ca répondra a mon problème parce que en ayant beaucoup cherché sur le stockage de fichier lisible par le navigateur, c'est un peu le néant ...

  12. #12
    Membre actif
    Inscrit en
    Décembre 2008
    Messages
    280
    Détails du profil
    Informations forums :
    Inscription : Décembre 2008
    Messages : 280
    Points : 261
    Points
    261
    Par défaut
    Salut,

    Voici le tuto promi :

    le tuto est ici !

    Si jamais tu as encore des problèmes, poste un commentaire sur le tuto ou ici.

    Paye Tes Dettes - Applciation android.

    DevHackSecure - Pense bête d'un étudiant en informatique - Tutos DEV

    " I also realize that _everybody_ thinks that they are right, and that they are supported by all other right-thinking people. That's just how people work. We all think we're better than average." Linus Torvalds

  13. #13
    Membre régulier
    Homme Profil pro
    Software Engineer
    Inscrit en
    Février 2013
    Messages
    139
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Software Engineer
    Secteur : High Tech - Électronique et micro-électronique

    Informations forums :
    Inscription : Février 2013
    Messages : 139
    Points : 94
    Points
    94
    Par défaut
    Ok, merci beaucoup je te tiens au courant rapidement !

  14. #14
    Membre régulier
    Homme Profil pro
    Software Engineer
    Inscrit en
    Février 2013
    Messages
    139
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Software Engineer
    Secteur : High Tech - Électronique et micro-électronique

    Informations forums :
    Inscription : Février 2013
    Messages : 139
    Points : 94
    Points
    94
    Par défaut
    Donc, comme je m'y attendais (avec moi l'informatique ne marche jamais du premier coup ...), le programme que j'ai tenté d'adapter à mon problème ne fonctionne pas :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    package com.example.hfzd;
     
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import android.app.Activity;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    import android.os.Environment;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    import android.widget.Toast;
     
     
    public class NoteActivity extends Activity{
     
    	final EditText text = (EditText) findViewById(R.id.texte_note);
        Button valider = (Button) findViewById(R.id.save);
        Button envoyer = (Button) findViewById(R.id.envoyer);
        final TextView letitre = (TextView) findViewById(R.id.titre_note);
        final String titre = letitre.getText().toString();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
        final String dateStr = simpleDateFormat.format(new Date());
        public static final int INTERNAL_STORAGE_MODE = 0;
    	private Context mContext;
    	private String mFileName;
    	private String mFilePath;
    	private String mFileFullPath;
    	private File mFile;
    	private int mMode;
     
    	    /** Called when the activity is first created. 
                 * @return */
    	    @Override
    	    public void onCreate(Bundle savedInstanceState) {
     
    	    	super.onCreate(savedInstanceState);
     
    	    	setContentView(R.layout.activity_note);
     
     
    			//////////////////////SAUVEGARDER FICHIER//////////////////////
     
    			  valider.setOnClickListener(new OnClickListener() {                
    			         public void onClick(View v) {                        
     
    			        	// Utiliser la class FileStorageHelper pour lire et écrire dans un fichier interne
    			     		// Use the FileStorageHelper to read and write in internal storage
    			        	 NoteActivity FSInternalHelper = new NoteActivity(mContext, ""+titre,Environment.DIRECTORY_DOWNLOADS, NoteActivity.INTERNAL_STORAGE_MODE);
    			     		FSInternalHelper.createFile();
    			     		FSInternalHelper.writeInInternalFile(""+text);
     
    			         }
     
    			  });
     
     
                //////////////////////SHARE CHAMP DE TEXTE//////////////////////
                envoyer.setOnClickListener(
                        new OnClickListener() {
                		@Override
                		public void onClick(View v) {
                			final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
                			emailIntent.setType("plain/text");
                			emailIntent.putExtra(Intent.EXTRA_SUBJECT, letitre.getText().toString());
                			emailIntent.putExtra(Intent.EXTRA_TEXT, text.getText().toString()+"_"+dateStr);
                			emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"thms.brdnv@gmail.com"});
                			NoteActivity.this.startActivity(Intent.createChooser(emailIntent, "Email Envoyer..."));
                		}
                        }
                );	 
    	    }
     
     
    	    public NoteActivity(Context mContext, String mFileName, String mFilePath, int mMode) {
    			super();
    			this.mContext = mContext;
    			this.mFileName = mFileName;
    			this.mMode = mMode;
    			this.mFileFullPath = this.mFileName;
    		}
     
    		public void createFile(){
    			// On prepare le fichier
    			// Prepare the file
    			this.mFile = new File(this.mFileFullPath);
    			try {
    				// Creer un fichier s'il n'existe pas
    				// Create new file if doesn't exist
    				this.mFile.createNewFile();
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		}
     
    		public void writeInInternalFile(String stringToWrite) {
    			try {
    				// Ouverture du flux d'écriture
    				// Open write stream
    				FileOutputStream output = this.mContext.openFileOutput(this.mFileFullPath, Context.MODE_WORLD_READABLE);
    				// On ecrit les données
    				// Writing data
    				output.write(stringToWrite.getBytes());
    				// On oubli pas de fermer le flux
    				// Don't forget to close the stream
    				output.close();
    			} catch (FileNotFoundException e) {
    				// File not found
    				// Fichier non trouvé
    				e.printStackTrace();
    			} catch (IOException e) {
    				// Les autres IO exception
    				// Any other IO exception
    				e.printStackTrace();
    			}
    		}
     
    		public String getFileName() {
    			return mFileName;
    		}
    		public String getFilePath() {
    			return mFilePath;
    		}
    		public void setFileName(String mFileName) {
    			this.mFileName = mFileName;
    		}
    		public void setFilePath(String mFilePath) {
    			this.mFilePath = mFilePath;
    		}
     
    }
    Je n'arrive même pas à ouvire la page de mon appli ou se trouve l'editeur de texte ...


    EDIT : Je pense que l'erreur vient du contexte que je ne maitrise pas trop encore mais je ne voit pas quoi changer ...

  15. #15
    Membre régulier
    Homme Profil pro
    Software Engineer
    Inscrit en
    Février 2013
    Messages
    139
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Software Engineer
    Secteur : High Tech - Électronique et micro-électronique

    Informations forums :
    Inscription : Février 2013
    Messages : 139
    Points : 94
    Points
    94
    Par défaut
    Ok, j'ai repris un ancien code qui j'ai pu faire fonctionner, il est beaucoup plus court et compréhensible (enfin à mon sens), je poste donc le code fonctionnel en espérant que ca puisse aider quelqu'un un jour :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    package com.example.hfzd;
     
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import android.app.Activity;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    import android.os.Environment;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    import android.widget.Toast;
     
     
    public class NoteActivity extends Activity{
     
     
     
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
        final String dateStr = simpleDateFormat.format(new Date());
     
     
    	    /** Called when the activity is first created. 
                 * @return */
    	    @Override
    	    public void onCreate(Bundle savedInstanceState) {
     
    	    	super.onCreate(savedInstanceState);
     
    	    	setContentView(R.layout.activity_note);
     
    	    	final EditText letext = (EditText) findViewById(R.id.texte_note);
    	    	final TextView letitre = (TextView) findViewById(R.id.titre_note);
     
    	        Button valider = (Button) findViewById(R.id.save);
    	        Button envoyer = (Button) findViewById(R.id.envoyer);
     
    			//////////////////////SAUVEGARDER FICHIER//////////////////////
     
    			  valider.setOnClickListener(new OnClickListener() {                
    			         public void onClick(View v) {                        			        	 
    			        	 ecrireFichier(letitre.getText().toString()+".txt",letext.getText().toString());
    			         }
     
    			  });
     
     
                //////////////////////SHARE CHAMP DE TEXTE//////////////////////
                envoyer.setOnClickListener(
                        new OnClickListener() {
                		@Override
                		public void onClick(View v) {
     
                			final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
                			emailIntent.setType("plain/text");
                			emailIntent.putExtra(Intent.EXTRA_SUBJECT, letitre.getText().toString()+"_"+dateStr);
                			emailIntent.putExtra(Intent.EXTRA_TEXT, letext.getText().toString());
                			emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{""});
                			NoteActivity.this.startActivity(Intent.createChooser(emailIntent, "Email Envoyer..."));
     
                		}
                        }
                );	 
    	    }
     
    	    //////////////////////SHARE CHAMP DE TEXTE//////////////////////
    	    public void ecrireFichier(String nomFichier,String monText) {
     
                BufferedWriter writer = null;
     
                try {
                		File dir = getExternalFilesDir("ToutMesFichiers");
                		dir.mkdirs();
                		File newfile = new File(dir,nomFichier);
                		writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(newfile)));
                		writer.write(monText);
                } catch (Exception e) {
                    Log.e("NoteActivity","Failed to write file",e);
                } finally {
                    if (writer != null) {
                        try {
                             writer.close();
                        } catch (IOException e) {
                             Log.e("NoteActivity","Failed to gracefully close output stream",e);
                        }
                    }
                }
    	    }
     
     
    }

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [XL-2003] Stockage fichier texte dans un tableau
    Par brownthefou dans le forum Macros et VBA Excel
    Réponses: 6
    Dernier message: 12/04/2012, 09h48
  2. Ecriture fichier texte impossible ?
    Par P.G dans le forum Débuter
    Réponses: 7
    Dernier message: 07/10/2011, 10h34
  3. [Debutant] stockage d'infos dans un fichier texte
    Par Cheeper dans le forum Entrée/Sortie
    Réponses: 4
    Dernier message: 12/01/2007, 11h02
  4. Réponses: 3
    Dernier message: 03/02/2006, 06h54
  5. Stockage de données & lecture d'un fichier texte
    Par petitours dans le forum C++Builder
    Réponses: 6
    Dernier message: 13/03/2004, 14h05

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo