【问题标题】:Reading .txt file from Asset Folder从资产文件夹中读取 .txt 文件
【发布时间】:2016-05-03 06:35:09
【问题描述】:

我正在通过传递上一个活动列表项中的字符串从 Asset 文件夹读取 .txt 文件。

让我们考虑一下,Recyclerview 项目大小为 5。

  1. Android -- 第 1 项
  2. IOS -- 项目 2
  3. 黑莓 -- 第 3 项
  4. Symbian -- 第 4 项
  5. 八达 -- 第 5 项

当我点击 Android 时,“Android”字符串通过 Intent 和 赋值给Song的变量。

 InputStream inputstream = getResources().getAssets().open(***Song*** + ".txt");

并从资产文件夹中读取 Android.txt 文件。仅调用第一项。当我单击列表中的其他项目时,intent String 传递给下一个活动,但没有从文件夹中打开相应的 .txt 文件。它跳起来接住。

这是我的代码。

 Intent i = getIntent();
        Song = i.getExtras().getString("Title");
        textView.setText(Song);
        System.out.println(".....1");
        try {
            System.out.println(".....2");
            InputStream inputstream = getResources().getAssets().open(Song + ".txt");
            System.out.println(".....3");
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputstream));
            String m = reader.toString();
            StringBuilder total = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                total.append(line);
            }
            System.out.println(".....4");
            m = total.toString();
            System.out.println(".....5" + m);

            textView.setText(m);
            textView.setTextColor(Color.BLACK);
            System.out.println(".....6");
        } catch (IOException ex) {
            System.out.println(".....7" + ex);
        }

System.out.println ---> 打印第一项点击中的所有内容

除了点击第一项,在 LogCat 中只打印以下内容。
System.out: .....1
System.out: .....2

在我单击RecyclerView 项的位置处的适配器类,

public class SongAdapter extends RecyclerView.Adapter<SongAdapter.ViewHolder> {
    Context con;

    ArrayList<String> songHeading = new ArrayList<>();
    SharedPreferences pref;
    String MyPREFERENCES = "Preference";


    public SongAdapter(Context context, ArrayList<String> heading) {
        this.con = context;
        this.songHeading = heading;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.song_list_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(v);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {

        pref = con.getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
        holder.txtSong.setText(songHeading.get(position));

        holder.Clicker.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(view.getContext(), MainActivity.class);
                i.putExtra("Title", songHeading.get(position));
                view.getContext().startActivity(i);
            }
        });
    }


    @Override
    public int getItemCount() {
        return songHeading.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public final View mView;
        public TextView txtSong;
        LinearLayout Clicker;

        public ViewHolder(View itemView) {
            super(itemView);
            mView = itemView;
            txtSong = (TextView) itemView.findViewById(R.id.textView);
            Clicker = (LinearLayout) itemView.findViewById(R.id.Clicker);
        }
    }
}

更新:

更改如下代码后在 Verbose 中出现错误

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.song_list_item2);
        context = this;

        pref = this.context.getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
        textView = (TextView) findViewById(R.id.textView);

        Intent i = getIntent();
        Song = i.getExtras().getString("Title");
        textView.setText(Song);
        System.out.println(".....1");
        textView.setText(loadJSONFromAsset());
    }

    private String loadJSONFromAsset() {
        String json = null;
        try {

            InputStream is = getAssets().open(Song + ".txt");

            int size = is.available();

            byte[] buffer = new byte[size];

            is.read(buffer);

            is.close();

            json = new String(buffer, "UTF-8");

        } catch (IOException ex) {
            ex.printStackTrace();
            return null;
        }
        return json;
    }

在 hitesh.androidjsonapp.MainActivity.loadJSONFromAsset(MainActivity.java:40) 指出这条线

   InputStream is = getAssets().open(Song + ".txt");

【问题讨论】:

  • 为什么不用字符串数组资源?
  • 我必须从 Assert 文件夹中读取 1000 多个文件。
  • 你没有提到。
  • 发布您的商品点击代码。您在其中创建意图并发送标题值
  • 如果我能读懂 5 个文件,即使超过 10000 个文件也很容易阅读。所以为了方便问题的进行,我提到了 5 个文件。

标签: android android-recyclerview assets


【解决方案1】:

确保文件名没有附加任何特殊字符(\n)或空格,并使用 try..catch 添加 finally 块,您需要关闭输入流

13588-13588/hitesh.androidjsonapp I/System.out: .....1 \n BlackBerry

【讨论】:

  • 其实\n是我编辑的。我得到的 Log 结果为 13588-13588/hitesh.androidjsonapp I/System.out: .....1 ---> 第一行 BlackBerry ---> 在第二行。通过向您显示单独的文本,我使用了 \n
  • 好的...我将使用 inputstream.close() 进行检查
  • :P 请检查并告诉我..确保文件名与资产文件夹中的文件名完全相同
  • 没有发现Result有任何变化。还是一样。
  • stackoverflow.com/a/9544781/1537419 你能检查一下这个答案吗,可能是字符编码给你造成了问题..
猜你喜欢
  • 2023-03-11
  • 1970-01-01
  • 2018-01-19
  • 2016-03-17
  • 2020-03-03
  • 1970-01-01
  • 2022-11-19
  • 2018-06-06
  • 2011-01-22
相关资源
最近更新 更多