CERCA
PER MODELLO
FullScreen Chatbox! :)

Utente del giorno: bluemask con ben 2 Thanks ricevuti nelle ultime 24 ore
Utente della settimana: bluemask con ben 2 Thanks ricevuti negli ultimi sette giorni
Utente del mese: megthebest con ben 28 Thanks ricevuti nell'ultimo mese

Pagina 2 di 3 primaprima 123 ultimoultimo
Ultima pagina
Visualizzazione dei risultati da 11 a 20 su 26
Discussione:

aprire un file .txt da res/raw

Se questa discussione ti è stata utile, ti preghiamo di lasciare un messaggio di feedback in modo che possa essere preziosa in futuro anche per altri utenti come te!
  1. #11
    Baby Droid


    Registrato dal
    Sep 2010
    Messaggi
    20

    Ringraziamenti
    0
    Ringraziato 2 volte in 2 Posts
    Predefinito

    no forse non ci stiamo capendo

    private String inputStreamToString(InputStream helpfile) {
    // TODO Auto-generated method stub
    return null;
    }


    questa funzione dovrebbe ritornare la stringa con cui vuoi valorizzare la textview. guarda prova a far ritornare una stringa fissa, tipo return "testo di prova"

  2.  
  3. #12
    Baby Droid


    Registrato dal
    Mar 2011
    Messaggi
    28

    Ringraziamenti
    3
    Ringraziato 1 volta in 1 Post
    Predefinito

    Se inserisco return "testo di prova" funzia.

  4. #13
    Baby Droid


    Registrato dal
    Sep 2010
    Messaggi
    20

    Ringraziamenti
    0
    Ringraziato 2 volte in 2 Posts
    Predefinito

    e certo.... in quella funzione devi tu scrivere il codice che ti estrae il testo dall'inputstream, ho fatto una rapida ricerca su google e ho trovato il seguente codice, vedi se lo puoi adattare alle tue esigenze

    codice:
        public String convertStreamToString(InputStream is)
                throws IOException {
            /*
             * To convert the InputStream to String we use the
             * Reader.read(char[] buffer) method. We iterate until the
             * Reader return -1 which means there's no more data to
             * read. We use the StringWriter class to produce the string.
             */
            if (is != null) {
                Writer writer = new StringWriter();
    
                char[] buffer = new char[1024];
                try {
                    Reader reader = new BufferedReader(
                            new InputStreamReader(is, "UTF-8"));
                    int n;
                    while ((n = reader.read(buffer)) != -1) {
                        writer.write(buffer, 0, n);
                    }
                } finally {
                    is.close();
                }
                return writer.toString();
            } else {        
                return "";
            }
        }

  5. Il seguente Utente ha ringraziato spampy78 per il post:

    Anular (28-03-11)

  6. #14
    Baby Droid


    Registrato dal
    Mar 2011
    Messaggi
    28

    Ringraziamenti
    3
    Ringraziato 1 volta in 1 Post
    Predefinito

    Ok, teoricamente l'ho capito. Sto provando ad implementarlo ma ho delle difficoltà, in particolare con i metodi autogenerati try/catch.

     
    1
    2
    setContentView(R.layout.help);
    3
            InputStream helpfile = getResources().openRawResource(R.raw.quizhelp);
    4
            TextView helpText = (TextView) findViewById(R.id.helptext);
    5
            String helpstring;
    6
            try {
    7
                helpstring = inputStreamToString(helpfile);
    8
                helpText.setText(helpstring);
    9
            } catch (IOException e) {
    10
                // TODO Auto-generated catch block
    11
                e.printStackTrace();
    12
            }
    13


    Ho dovuto implementarli xkè mi dava un errore (e quindi il consiglio try/catch)sul metodo inputStreamToString.

    29
     
    1
    private String inputStreamToString(InputStream helpfile) throws IOException {
    2
            // TODO Auto-generated method stub
    3
        
    4
    /*
    5
     * To convert the InputStream to String we use the
    6
     * Reader.read(char[] buffer) method. We iterate until the
    7
     * Reader return -1 which means there's no more data to
    8
     * read. We use the StringWriter class to produce the string.
    9
     */
    10
    if (helpfile != null) {
    11
        Writer writer = new StringWriter();
    12
    13
       
    14
        char[] buffer = new char[1024];
    15
        try {
    16
            Reader reader = new BufferedReader(
    17
                    new InputStreamReader(helpfile, "UTF-8"));
    18
            int n;
    19
            while ((n = reader.read(buffer)) != -1) {
    20
                writer.write(buffer, 0, n);
    21
            }
    22
        } finally {
    23
            helpfile.close();
    24
        }
    25
        return writer.toString();
    26
    } else {        
    27
        return "";
    28
    }
    29
    }



    Spero di non aver scritto un mucchio di stronzate!

  7. #15
    Baby Droid


    Registrato dal
    Sep 2010
    Messaggi
    20

    Ringraziamenti
    0
    Ringraziato 2 volte in 2 Posts
    Predefinito

    Si effettivamente nel codice che avevo postato manca il blocco catch. Implementa sia il catch che il finally. Era questo il dubbio?

  8. #16
    Baby Droid


    Registrato dal
    Mar 2011
    Messaggi
    28

    Ringraziamenti
    3
    Ringraziato 1 volta in 1 Post
    Predefinito

    ancora niente. Pagina totalmente vuota.

    54
     
    1
    public class HelpActivity extends Activity {
    2
        /** Called when the activity is first created. */
    3
        @Override
    4
        public void onCreate(Bundle savedInstanceState) {
    5
            super.onCreate(savedInstanceState);
    6
            setContentView(R.layout.help);
    7
            InputStream helpfile = getResources().openRawResource(R.raw.quizhelp);
    8
            TextView helpText = (TextView) findViewById(R.id.helptext);
    9
            InputStream helpstring;
    10
            try {
    11
                helpstring = inputStreamToString(helpfile);
    12
                            System.out.println("ciao_string");
    13
    (non stampa)
    14
    15
            } catch (IOException e) {
    16
                // TODO Auto-generated catch block
    17
                e.printStackTrace();
    18
            }
    19
            
    20
        
    21
        }
    22
    23
        private InputStream inputStreamToString(InputStream helpfile) throws IOException {
    24
            // TODO Auto-generated method stub
    25
        
    26
    /*
    27
     * To convert the InputStream to String we use the
    28
     * Reader.read(char[] buffer) method. We iterate until the
    29
     * Reader return -1 which means there's no more data to
    30
     * read. We use the StringWriter class to produce the string.
    31
     */
    32
    if (helpfile != null) {
    33
        Writer writer = new StringWriter();
    34
    35
       
    36
        char[] buffer = new char[1024];
    37
        try {
    38
            Reader reader = new BufferedReader(
    39
                    new InputStreamReader(helpfile, "UTF-8"));
    40
            int n;
    41
            while ((n = reader.read(buffer)) != -1) {
    42
                writer.write(buffer, 0, n);
    43
                System.out.println("ciao");
    44
             (Non stampa!)
    45
            }
    46
        } finally {
    47
            helpfile.close();
    48
        }
    49
        return helpfile;
    50
    } else {        
    51
        return null;
    52
    }
    53
    }
    54
    }

  9. #17
    Baby Droid


    Registrato dal
    Sep 2010
    Messaggi
    20

    Ringraziamenti
    0
    Ringraziato 2 volte in 2 Posts
    Predefinito

    Appena torno a casa stasera faccio qualche prova e ti faccio sapere

  10. Il seguente Utente ha ringraziato spampy78 per il post:

    Anular (28-03-11)

  11. #18
    Baby Droid


    Registrato dal
    Sep 2010
    Messaggi
    20

    Ringraziamenti
    0
    Ringraziato 2 volte in 2 Posts
    Predefinito

    Quote Originariamente inviato da Anular Visualizza il messaggio
    ancora niente. Pagina totalmente vuota.

    54
     
    1
    public class HelpActivity extends Activity {
    2
        /** Called when the activity is first created. */
    3
        @Override
    4
        public void onCreate(Bundle savedInstanceState) {
    5
            super.onCreate(savedInstanceState);
    6
            setContentView(R.layout.help);
    7
            InputStream helpfile = getResources().openRawResource(R.raw.quizhelp);
    8
            TextView helpText = (TextView) findViewById(R.id.helptext);
    9
            InputStream helpstring;
    10
            try {
    11
                helpstring = inputStreamToString(helpfile);
    12
                            System.out.println("ciao_string");
    13
    (non stampa)
    14
    15
            } catch (IOException e) {
    16
                // TODO Auto-generated catch block
    17
                e.printStackTrace();
    18
            }
    19
            
    20
        
    21
        }
    22
    23
        private InputStream inputStreamToString(InputStream helpfile) throws IOException {
    24
            // TODO Auto-generated method stub
    25
        
    26
    /*
    27
     * To convert the InputStream to String we use the
    28
     * Reader.read(char[] buffer) method. We iterate until the
    29
     * Reader return -1 which means there's no more data to
    30
     * read. We use the StringWriter class to produce the string.
    31
     */
    32
    if (helpfile != null) {
    33
        Writer writer = new StringWriter();
    34
    35
       
    36
        char[] buffer = new char[1024];
    37
        try {
    38
            Reader reader = new BufferedReader(
    39
                    new InputStreamReader(helpfile, "UTF-8"));
    40
            int n;
    41
            while ((n = reader.read(buffer)) != -1) {
    42
                writer.write(buffer, 0, n);
    43
                System.out.println("ciao");
    44
             (Non stampa!)
    45
            }
    46
        } finally {
    47
            helpfile.close();
    48
        }
    49
        return helpfile;
    50
    } else {        
    51
        return null;
    52
    }
    53
    }
    54
    }
    ciao scusami ma non ho potuto ancora avuto tempo di provare il codice, però ad occhio e croce ad uno sguardo veloce....

    penso dovresti cambiare
    codice:
    helpstring = inputStreamToString(helpfile);
    in
    codice:
    helpText.setText(inputStreamToString(helpfile));
    e
    codice:
    return helpfile;
    in
    codice:
    return (writer.toString());

  12. #19
    Baby Droid


    Registrato dal
    Mar 2011
    Messaggi
    28

    Ringraziamenti
    3
    Ringraziato 1 volta in 1 Post
    Predefinito

    nada. sta diventando un'impresa

  13. #20
    Baby Droid


    Registrato dal
    Sep 2010
    Messaggi
    20

    Ringraziamenti
    0
    Ringraziato 2 volte in 2 Posts
    Predefinito

    allora... ho testato velocemente il seguente codice e funziona tutto, confrontalo con il tuo

    codice:
    package myTest.test;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.Reader;
    import java.io.StringWriter;
    import java.io.Writer;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.TextView;
    
    public class main extends Activity 
    {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            
            InputStream helpfile = getResources().openRawResource(R.raw.indirizzi);
            TextView helpText = (TextView) findViewById(R.id.TextView01);
            
            try
            {
            	helpText.setText(inputStreamToString(helpfile));
            }
            catch (IOException e)
            {
            	e.printStackTrace();
            }
        }
    
        private String inputStreamToString(InputStream helpfile) throws IOException 
        {  
    		if (helpfile != null) 
    		{  
    		    Writer writer = new StringWriter();  
    		  
    		    char[] buffer = new char[1024];  
    		    
    		    try 
    		    {  
    		        Reader reader = new BufferedReader(new InputStreamReader(helpfile, "UTF-8"));  
    		        int n;
    		        while ((n = reader.read(buffer)) != -1) 
    		        {  
    		            writer.write(buffer, 0, n);  
    		            System.out.println("ciao");  
    		        }  
    		    }
    		    catch (IOException e)
    		    {
    		    	e.printStackTrace();
    		    }
    		    finally 
    		    {  
    		        helpfile.close();  
    		    }  
    		    
    		    return writer.toString();  
    		} 
    		else 
    		{          
    		    return null;  
    		}
        }
    }

Pagina 2 di 3 primaprima 123 ultimoultimo
Ultima pagina

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire risposte
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Torna su
Privacy Policy