Android Apidemos Sample resolution: Content->assets->read Asset

Source: Internet
Author: User
Tags int size resource

Android. The content package defines classes that are primarily used to access or publish data on devices, consisting of three of packages.

Content Sharing (android.content) is primarily used to share some data with each part of the application, and the main column has content provider,contentresolver for managing and publishing data. Intent,intentfilter is used to send messages between different components application.

Package Management (ANDROID.CONTENT.PM) is used to access the activities, Permissions, Services, signatures, and providers defined by the Android Package (. apk). The main class is Packagemanager.

Resource Management (Android.content.res) is used to access resources defined in the application, such as Strings,drawables, Media,device configuration, and so on, and the primary class is resource.

In general application the resources under the Res directory, in some cases application need to use some custom files, one way is to put them under the Res/raw directory, the other way is to put them in the assets directory, and Res Unlike directories, the Android SDK does not generate a resource ID for the files under the directory assets, but rather assetmanager the file name to the/assets directory organization, using Assetmanager access The same file structure is used. Assets provides a lower level of resource access than Res.

This example uses Assetmanager to access the Read_asset.txt in the assets directory.

InputStream is = Getassets (). Open ("Read_asset.txt");      

We guarantee that this available method returns the total
//size of the asset ...  Of course, this does mean the a single
//asset can ' t is more than 2 gigs.
int size = is.available ();

Read the entire asset into a local byte buffer.
byte[] buffer = new Byte[size];
Is.read (buffer);
Is.close ();

Convert the buffer into a string.
String text = new string (buffer);

Finally stick the string into the text view.
TextView TV = (TextView) Findviewbyid (r.id.text);
Tv.settext (text);

Assetmanager objects can be obtained by getassets () in activity, and file operations like Assetmanager can be read by byte stream.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.