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

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    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
    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 confirmé
    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
    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 confirmé

    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
    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 ?

  4. #4
    Membre confirmé
    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
    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 confirmé

    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
    Billets dans le blog
    3
    Par défaut
    Là encore... tout est expliqué dans la documentation sur les external-storage

  6. #6
    Membre confirmé
    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
    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 ...

+ 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