在Android应用程序中,有的资源文件是在工程内的,有的资源文件是在工程外的,如何读取这些资源,我将定期的归纳总结。
1.读取Img资源
1.1在工程内通过资源名(文件名)取得资源ID。
// google为文件名,不需要后缀,drawable是资源的类型(可以是xml,string,values,layout),后面一个参数是包名。
int resId = this.getResources().getIdentifier("google", "drawable", this.getPackageName());
1.2在工程外引用资源文件
String url = "http://www.baidu.com/img/baidu_jgylogo3.gif";
try {
URLConnection conn = new URL(url).openConnection();
conn.setConnectTimeout(5000);
conn.setReadTimeout(5000);
conn.connect();
InputStream inputStream = conn.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
inputStream.close();
ImageView p_w_picpath = (ImageView) this.findViewById(R.id.menuImage);
p_w_picpath.setImageBitmap(bitmap);
} catch (Exception e) {
return;
}
转载于:https://blog.51cto.com/nami007/702141