使用参数时无法检索文件

时间:2018-06-14 05:02:24

标签: java android

在传递参数以搜索文件时遇到问题。 基本上,当尝试读取文件时,此代码有效:

private void setContent(){
        String text = "";
        String randomText = "";

try
{

InputStream input = getAssets().open("files/jokes.txt");

            int size = input.available();
            byte[] buffer = new byte[size];
            input.read(buffer);
            input.close();
            // byte buffer into a string
            text = new String(buffer);

            String[] splitText = text.split("###");
            Random rand = new Random();
            int  randomIndex = rand.nextInt(splitText.length);
            randomText = splitText[randomIndex];

        }
        catch (Exception e) {
            System.out.println(e);
        }

        contentText.setText(randomText);

    }

注意上面的InputStream。我的文本文件存储在asssets / files / jokes.txt。

现在我尝试使用一个parmatised文件名的intent,它总是命中catch语句,文件名被捕获为""当它过去时:

主要活动:

jokesButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            openContentPage("jokes");
        }
    });

    poemsButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            openContentPage("poems");
        }
    });

    funnyStoriesButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            openContentPage("funnyStories");
        }
    });

...

  private void openContentPage(String v) {
        if (v.equals("jokes")) {
            Intent intentContentPage = new Intent(MainActivity.this, Content.class);
            intentContentPage.putExtra("keyPage", "jokes");
            startActivity(intentContentPage);
        } else if (v.equals("poems")) {
            Intent intentContentPage = new Intent(MainActivity.this, Content.class);
            intentContentPage.putExtra("keyPage", "poems");
            startActivity(intentContentPage);
        } else if (v.equals("funnyStories")) {
            Intent intentContentPage = new Intent(MainActivity.this, Content.class);
            intentContentPage.putExtra("keyPage", "funnyStories");
            startActivity(intentContentPage);
        }

内容:

 private void setContent(){
        String text = "";
        String randomText = "";
        String keyPageValue = getIntent().getStringExtra("keyPage");

        String fileName = "";

        if(keyPageValue.equals("jokes") ){
            fileName.equals("files/jokes.txt");
        }
        else if (keyPageValue.equals("poems")){
            fileName.equals("files/poems.txt");
        }
        else if (keyPageValue.equals("funnyStories")){
            fileName.equals("files/funnystories.txt");
        }

        try {

            InputStream input = getAssets().open(fileName);

            int size = input.available();
            byte[] buffer = new byte[size];
            input.read(buffer);
            input.close();
            // byte buffer into a string
            text = new String(buffer);

            String[] splitText = text.split("###");
            Random rand = new Random();
            int  randomIndex = rand.nextInt(splitText.length);
            randomText = splitText[randomIndex];

        }
        catch (Exception e) {
            System.out.println(e);
        }

        contentText.setText(randomText);

    }

为什么不选择它?如前所述,所有文件都存储在assets / files文件夹中。

0 个答案:

没有答案