0

I seem to be overlooking something quite obvious with my loop, but in its present state it is not posting the content of the eula text file to the alert dialog. Anyone see anything I am overlooking? There are 21 lines in the text file. Thanks!

Resources res = getResources();
InputStream in_s = res.openRawResource(R.raw.eula);
BufferedReader reader = new BufferedReader(new InputStreamReader(in_s));

    String key1 = preference.getKey();
    //if the user click on the Legal Notices preference, display the license
    if (key1.equalsIgnoreCase("prefEULA")){
    String eulaContent = null;
    try {

        while ((reader.readLine()) != null)
            for(int i=0;i<21;i++){
        {
             eulaContent = reader.readLine();

    }

    }
    }   catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Builder eulaDialog = new AlertDialog.Builder(this);
    eulaDialog.setTitle("End-User Licence Agreement");
    if (eulaContent!=null){
    eulaDialog.setMessage(eulaContent);
    eulaDialog.show();                

    }            

    }
user3391426
  • 433
  • 1
  • 5
  • 17

2 Answers2

2

Update: the problem is the method openRawResource. It fails on string content

remove the for loop and try to use the += operator

Resources res = getResources();
InputStream in_s = res.openRawResource(R.raw.eula);
BufferedReader reader = new BufferedReader(new InputStreamReader(in_s));

String key1 = preference.getKey();
//if the user click on the Legal Notices preference, display the license
if (key1.equalsIgnoreCase("prefEULA")){
String eulaContent = "";
try {

    while ((reader.readLine()) != null)
    {
         eulaContent += reader.readLine();

}
}   catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
Builder eulaDialog = new AlertDialog.Builder(this);
eulaDialog.setTitle("End-User Licence Agreement");
if (eulaContent!=null){
eulaDialog.setMessage(eulaContent);
eulaDialog.show();                

}            

}
Augusto
  • 1,234
  • 1
  • 16
  • 35
  • I'm getting `nullnull` in the dialog. – user3391426 Mar 11 '14 at 22:38
  • 1
    I think the problem might be the way you're reading the file. The Android Doc says openRawResource does not work with strings – Augusto Mar 11 '14 at 22:51
  • I think you should try this aproach using ExternalStorageDirectory. http://stackoverflow.com/questions/12421814/how-to-read-text-file-in-android – Augusto Mar 11 '14 at 22:54
  • What the user doesn't have the file on their SD card? – user3391426 Mar 12 '14 at 02:26
  • use the internal storage then. it will keep the file hidden for the user: http://developer.android.com/guide/topics/data/data-storage.html#filesInternal – Augusto Mar 12 '14 at 03:54
  • I would like to vote your answer up +1 for getting me on the right track, but I opted to do things slightly different, so that I could largely keep the loop I had already written. Thanks! – user3391426 Mar 14 '14 at 19:05
  • Please write the answer of your question and accept it for someone that might have this problem in the future – Augusto Mar 14 '14 at 19:07
  • Re-examining my code, I guess it is closer to your answer than I originally thought. It just needed to be further tweaked a bit. Thanks, again! – user3391426 Mar 14 '14 at 19:14
0

I really wanted to use the loop I had already written, and was able to once I got some advice that put me on the right track. I ended up going with the code below, which works awesomely! A few adjustments to the loop, which included adding a null String object for the buffered data to be appended to. Also, the ...+ "\n"; bit adds the new lines back into the buffered data.

 Resources res = getResources();  
 InputStream in_s = res.openRawResource(R.raw.eula);  
 BufferedReader reader = new BufferedReader(new InputStreamReader(in_s));  

        String key = preference.getKey();  
        //if the user click on the Legal Notices preference, display the license  
        if (key.equalsIgnoreCase("prefEULA")){  
        String eulaContent = "";  
        try {  

            String line = null;  
            while ((line = reader.readLine()) != null) {  

            eulaContent += line + "\n";
        }  


        }   catch (IOException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
        Builder eulaDialog = new AlertDialog.Builder(this);  
        eulaDialog.setTitle("End User License Agreement");  
        if (eulaContent!=null){  
        eulaDialog.setMessage(eulaContent);  
        eulaDialog.show();                  

        }              

        }  
user3391426
  • 433
  • 1
  • 5
  • 17