Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to read Resource Files in Android

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "how to read resource files in Android". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Put it in the resource folder, that is, under the res of the android project you created.

We can set up a folder in it and place the files we want to read. For example: create a directory raw in res, put the file abc.bin to be read into this folder, and then read it through the openRawResource () method. The code is

InputStream is = getResources () .openRawResource (R.raw.abc)

As long as you get InputStream, everything will be easy!

Note: the directory name under res is fastidious. The directory template booked by the system is located under framework/base/core/res/res and must be created according to the name inside. That is, rewrite the directory of the system! The naming of the resource files below can only be named with [a_z0-9], otherwise an error will be reported.

2. Use the openFileInput () method

Because it is a method inside ContextWrapper, its direct subclass contains Activity, so it can be easily used in Activity!

Resources cannot contain path separators (can not contain path separators) and can only be placed under the folder / data/data/ package name / files. If the file is not placed in this directory or a path name is added, a FileNotFoundException will be thrown.

Char [] inputBuffer = new char [2048]; FileInputStream fis = this.openFileInput ("Version_Num.BIN"); InputStreamReader is = new InputStreamReader (myFileStream); / / start reading fis.read (inputBuffer); String data = new String (inputBuffer); that's all for "how to read resource files in Android". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report