CharSequence类、AssetManager类、Context类、Resource类的用法

CharSequence类型
这是一个接口,代表的是一个有序字符集合,这个接口包含的方法有:charAt(int index),toString(),length(),subSequence(int start,int end).
这里需要说的一点就是,对于一个抽象类或者是接口类,不能使用new来进行赋值,但是可以通过以下的方式来进行实例的创建:
CharSequence cs="hello";
但是不能这样来创建:
CharSequence cs=new CharSequence("hello");
下面来看看一个例子:
TextView tv;    //声明一个TextView类型的变量tv
CharSequence cs;    //声明一个CharSequence类型的变量cs
String str;    //声明一个字符串类型的str变量
cs=getText(R.string.styled_text);    //其实这里使用的this.getText()方法,即指定上下文的方法
tv=(TextView)findViewById(R.id.styled_text);    //通过给定的id将tv与对应的组件联系起来
tv.setText(cs);        //使用这句代码来设置tv的显示内容

str=getString(R.string.styled_text);
tv=(TextView)findViewById(R.id.plain_text);
tv.setText(str);

Context context=this;    //这里使用了Context类型的变量context,指定为当前上下文
Resources res=context.getResources();        //定义一个Resources类型的变量res,并给它赋值
cs=res.getText(R.string.styled_text);        //获得R类中指定变量的值
tv=(TextView)findViewById(R.id.styled_text);    //同上
tv.setText(cs);            //设置值


下面来看看如何在Android应用程序中访问文件应用程序包中的资源
AssetManager类,即管理资产类,这个类为访问当前应用程序的资产文件提供了入口。
这个类的方法有:open (String filename,int accessMode)使用一个精确的访问模式来打开当前包的一个资产,
返回输入流,即由此读取了这个包的资产的内容。要注意的是,这里所说的资产是放置在assets目录下的文件资产。
其中accessmode的值可以为:ACCESS_BUFFER,ACCESS_RANDOM,ACCESS_STREAMING,ACCESS_UNKNOWN其中的一个。
下面给出一个实例:
InputStream is=getAssets().open(String filename);//从指定的filename文件中读取内容,并将其放入到InputStream类型的is变量中
int size=is.available(); //获取流的长度
byte[] buffer=new byte[size];     //定义流长度的字节数组
is.read(buffer);    //将流中的内容放到buffer数组中
is.close();        
String text=new String(buffer);
TextView tv=(TextView)findViewById(R.id.text);
tv.setText(text);        //同上

 

 

Android 除了提供/res目录存放资源文件外,在/assets目录也可以存放资源文件,而且/assets目录下的资源文件不会在R.java自动生成ID,所以读取/assets目录下的文件必须指定文件的路径。我们可以通过AssetManager 类来访问这些文件。

 

比如我要读取/assets/background.png

 

 

Java代码
  1. Bitmap bgImg = getImageFromAssetFile( "background.png" );  
Bitmap bgImg = getImageFromAssetFile("background.png");
 

 

Java代码
  1.        private  Bitmap getImageFromAssetFile(String fileName){  
  2.     Bitmap image = null ;  
  3.     try {  
  4.         AssetManager  am = context.getAssets();  
  5.         InputStream is = am.open(fileName);  
  6.         image = BitmapFactory.decodeStream(is);  
  7.         is.close();  
  8.     }catch (Exception e){  
  9.           
  10.     }  
  11.     return  image;  

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
Android系统下载管理DownloadManager增强方法,可用于包括获取下载相关信息,如: getStatusById(long) 得到下载状态 getDownloadBytes(long) 得到下载进度信息 getBytesAndStatus(long) 得到下载进度信息和状态 getFileName(long) 得到下载文件路径 getUri(long) 得到下载uri getReason(long) 得到下载失败或暂停原因 getPausedReason(long) 得到下载暂停原因 getErrorCode(long) 得到下载错误码 =================================================================== package cn.trinea.android.common.util; import java.lang.reflect.Method; import android.app.DownloadManager; import android.app.DownloadManager.Request; import android.database.Cursor; import android.net.Uri; import android.os.Build; /** * DownloadManagerPro * * Get download info * {@link #getStatusById(long)} get download status * {@link #getDownloadBytes(long)} get downloaded byte, total byte * {@link #getBytesAndStatus(long)} get downloaded byte, total byte and download status * {@link #getFileName(long)} get download file name * {@link #getUri(long)} get download uri * {@link #getReason(long)} get failed code or paused reason * {@link #getPausedReason(long)} get paused reason * {@link #getErrorCode(long)} get failed error code * * * Operate download * {@link #isExistPauseAndResumeMethod()} whether exist pauseDownload and resumeDownload method in * {@link DownloadManager} * {@link #pauseDownload(long...)} pause download. need pauseDownload(long...) method in {@link DownloadManager} * {@link #resumeDownload(long...)} resume download. need resumeDownload(long...) method in {@link DownloadManager} * * * RequestPro * {@link RequestPro#setNotiClass(String)} set noti class * {@link RequestPro#setNotiExtras(String)} set noti extras * * * @author Trinea 2013-5-4 */ public class DownloadManagerPro { public static final Uri CONTENT_URI

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

offbye

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值