Android access to resources under the asset directory

Source: Internet
Author: User

Android provides assetmanager to access resources under the Asset directory,

Get Assetmanager through Getassets () in activity

The common APIs are as follows:

1, enumerate the resources under the path string[] List (String path)

2. InputStream Open (Resource path under Asset directory)

Here is the code to ask the picture under the Asset directory

Package Com.example.qunzheng.customerview;import Android.app.activity;import Android.content.res.AssetManager; Import Android.graphics.bitmapfactory;import Android.graphics.drawable.bitmapdrawable;import android.os.Bundle; Import Android.view.view;import android.widget.imageview;import java.io.ioexception;import java.io.InputStream;    public class Visitassetsresourceactivity extends Activity {private ImageView ImageView;    Private string[] images = null;    Private Assetmanager Assetmanager;    private int curimageindex = 0;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_visit_assets_resource);        ImageView = (ImageView) Findviewbyid (r.id.image);        Assetmanager = Getassets ();        try {images = Assetmanager.list ("");        } catch (IOException e) {e.printstacktrace (); }} public void Viewimage (view view) {if (Curimageindex; = images.length) {curimageindex = 0; /** * If the picture resource is not released, release the resource to prevent memory overflow */bitmapdrawable drawable = (bitmapdrawable) imageview.ge        Tdrawable (); if (drawable! = null && drawable.getbitmap ()! = null &&!drawable.getbitmap (). isRecycled ()) {D        Rawable.getbitmap (). Recycle ();            } try {InputStream assertfile = Assetmanager.open (images[curimageindex++]);        Imageview.setimagebitmap (Bitmapfactory.decodestream (assertfile));        } catch (IOException e) {e.printstacktrace (); }    }}

  

Android access to resources under the asset directory

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.