user1108339
user1108339

Reputation: 121

Text Resources in Android sdk

I have book with lessons about android . Now i realize help screen when i have many text . In book type about text . that android sdk can use txt files with many text . and in book typed code but when im use him , it doesent work (my app launch but i dont have any text on screen ) what's wrong? Help with right variant . my code :

package com.lineage.goddess;

import java.io.InputStream;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class LineageHelpActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.help);
        openRawResource();
        InputStream iFile = getResources().openRawResource(R.raw.lineagehelp);
        TextView helpText =(TextView) findViewById(R.id.TextView_HelpText);
        String strFile = inputStreamToString(iFile);
        helpText.setText(strFile);
    }

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

    private void openRawResource() {
        // TODO Auto-generated method stub

    }
}

Upvotes: 0

Views: 184

Answers (2)

mihail
mihail

Reputation: 2173

change

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

with

private String inputStreamToString(InputStream iFile) 
{               
    Writer writer = new StringWriter();
    if(iFile!=null)
    {
        char[] buffer = new char[1024];
        try{
            Reader reader = new BufferedReader(
                    new InputStreamReader(iFile, "UTF-8"));
            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }
        } catch (UnsupportedEncodingException e) {
            return e.toString();
        } catch (IOException e) {
            return e.toString();
        }finally
        {
            try {
                iFile.close();
            } catch (IOException e) {
                return e.toString();
            }
        }
    }
   String result = writer.toString();
   return result;
}

Upvotes: 0

Chris Cashwell
Chris Cashwell

Reputation: 22859

Well... for one, you are returning null from inputStreamToString(...). You need to implement it correctly before you can expect it to return anything.

Upvotes: 1

Related Questions