在J2ME中,我这样做是这样的: getClass().getResourceAsStream("/raw_resources.dat");
但是在android中,我总是对此无效,为什么呢?
InputStream raw = context.getAssets().open("filename.ext");
Reader is = new BufferedReader(new InputStreamReader(raw, "UTF8"));
对于原始文件,您应该考虑在res目录中创建一个原始文件夹,然后getResources().openRawResource(resourceName)从您的活动中调用。
在某些情况下,如果生成ID,则必须使用图像名称从可绘制或原始文件夹中获取图像
// Image View Object
mIv = (ImageView) findViewById(R.id.xidIma);
// create context Object for to Fetch image from resourse
Context mContext=getApplicationContext();
// getResources().getIdentifier("image_name","res_folder_name", package_name);
// find out below example
int i = mContext.getResources().getIdentifier("ic_launcher","raw", mContext.getPackageName());
// now we will get contsant id for that image
mIv.setBackgroundResource(i);
TextView txtvw = (TextView)findViewById(R.id.TextView01);
txtvw.setText(readTxt());
private String readTxt()
{
InputStream raw = getResources().openRawResource(R.raw.hello);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int i;
try
{
i = raw.read();
while (i != -1)
{
byteArrayOutputStream.write(i);
i = raw.read();
}
raw.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return byteArrayOutputStream.toString();
}
在res / raw文件夹中的TextView01 :: txtview在hello ::。txt文件中(您可以通过wel访问ny othr文件夹)
第2行是用onCreate()方法编写的2行
剩下的要写在扩展Activity的课堂上!
举报