Search code examples
androidassetsfile-read

Can't read text file from assets with button click


i've seen this question is common, but after searching i couldnt figure out the solution to my problem: the goal is to read a simple text file from assets when a button is clicked. I've followed this tutorial, adapted to my project, but when the button is clicked, nothing happens, although the file is in the right place. Here's the code and thanks in advance:

public class ResultActivity extends Activity implements View.OnClickListener {

Button restart;
Button answers;
TextView ler;
TextView msg;

 @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_result);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    msg = (TextView) findViewById(R.id.msg);
    ler=(TextView) findViewById(R.id.ler);
    answers=(Button) findViewById(R.id.answers);
    answers.setOnClickListener(this);
    restart=(Button) findViewById(R.id.restartQuiz);
    restart.setOnClickListener(this);

    msg.setText("Correct Answers: " + QuizActivity.correct + "Wrong Answers: "
            + QuizActivity.wrong + " Your Final Score is " + QuizActivity.score);


}

@Override
public void onClick(View view) {
    if (view == restart) {

        QuizActivity.score=0;
        QuizActivity.correct=0;
        QuizActivity.wrong=0;
        Intent a = new Intent(this, MainActivity.class);
        startActivity(a);
    }
    if(view==answers){
        String text="";
        try{
            InputStream is= getAssets().open("file.txt");
            int size=is.available();
            byte [] buffer=new byte[size];
            is.read(buffer);
            is.close();
        }catch (IOException e){
            e.printStackTrace();
        }
        ler.setText(text);
    }
  }

 public void onBackPressed() {

  }
}

Solution

  • This is code may help you a class whose implementation is provided by the Android system. It allows access to application-specific resources and classes. Return an AssetManager instance for your application's package.

    public class ResultActivity extends Activity implements View.OnClickListener {
    
     Button restart;
     Button answers;
     TextView ler;
     TextView msg;
    
     @Override
     protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_result);
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    
    msg = (TextView) findViewById(R.id.msg);
    ler=(TextView) findViewById(R.id.ler);
    answers=(Button) findViewById(R.id.answers);
    answers.setOnClickListener(this);
    restart=(Button) findViewById(R.id.restartQuiz);
    restart.setOnClickListener(this);
    
    msg.setText("Correct Answers: " + QuizActivity.correct + "Wrong Answers: "
            + QuizActivity.wrong + " Your Final Score is " + QuizActivity.score);
    
    
    }
    
     @Override
     public void onClick(View view) {
     if (view == restart) {
    
        QuizActivity.score=0;
        QuizActivity.correct=0;
        QuizActivity.wrong=0;
        Intent a = new Intent(this, MainActivity.class);
        startActivity(a);
       }
       if(view==answers){
        String text="";
        try{
    AssetManager text = myContext.getAssets();
        InputStream is = text.open("file.txt");
            InputStream is= getAssets().open("file.txt");
            int size=is.available();
            byte [] buffer=new byte[size];
            is.read(buffer);
    
           text = new String(buffer, 0, size); //this line was missing
    
            is.close();
        }catch (IOException e){
            e.printStackTrace();
        }
        ler.setText(text);
      }
    }
    
    public void onBackPressed() {
    
     }
    }