2019独角兽企业重金招聘Python工程师标准>>>
在某些应用中,为了实现应用apk资源放入重复利用,或者使用反射得到本应用的资源,需要使用反射反射方式获得,但Resources类中也自带了这种获取方式,并且功能更加强大
你可以获取string,color,drawable,raw,xml等文件,因此也就意味着,这里可以获取的资源是res中已定义的资源,对于控件id的获取,暂时无法做到
android.content.res.Resources.class
public int getIdentifier(String name, String defType, String defPackage) {if (name == null) {throw new NullPointerException("name is null");}try {return Integer.parseInt(name);} catch (Exception e) {// Ignore}return mAssets.getResourceIdentifier(name, defType, defPackage);}
一.获取资源的id
1.如下,我们可以获取当前应用的资源id
int drawableId = mContext.getResources().getIdentifier("ic_launcher","drawable", mContext.getPackageName());mImageView.setImageResource(drawableId);
2.我们也可以获取其他应用的资源id
Resources resources = context.getResources();
int indentify= getResources().getIdentifier("icon", "drawable", "org.anddev.android.testproject");int resId = getResources().getIdentifier("background", "color", getPackageName());
startBtn.setTextColor(getResources().getColor(resId));
对于这种方式,我们也可以这么做
int indentify = getResources().getIdentifier("org.loveandroid.androidtest:drawable/icon",null,null);
3.进行封装一下
public static int getResourceId(Context context,String name,String type,String packageName){Resources themeResources=null;PackageManager pm=context.getPackageManager();try {themeResources=pm.getResourcesForApplication(packageName);return themeResources.getIdentifier(name, type, packageName);} catch (NameNotFoundException e) {e.printStackTrace();}return 0;}
二.获取资源的uri
android系统中,应用的资源存储时也通常会被存入 数据库,也可以被共享,因此来说资源会获得应用的uri
Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.xinyueshenhua);
Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.drawable.ic_launcher);
我们也可以进一步封装
public static Uri getResourceUri(int resId,String packageName)
{return Uri.parse("android.resource://"+packageName+"/"+resId);
}
比如获取图片
Uri mImageCaptureUri = data.getData();
Bitmap photoBmp = null;
if (mImageCaptureUri != null) {photoBmp = MediaStore.Images.Media.getBitmap(ac.getContentResolver(), mImageCaptureUri);
}
或如下方式
/** * 通过uri获取图片并进行压缩 * * @param uri */ public static Bitmap getBitmapFormUri(Activity ac, Uri uri) throws FileNotFoundException, IOException { InputStream input = ac.getContentResolver().openInputStream(uri); BitmapFactory.Options onlyBoundsOptions = new BitmapFactory.Options(); onlyBoundsOptions.inJustDecodeBounds = true; onlyBoundsOptions.inDither = true;//optional onlyBoundsOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;//optional BitmapFactory.decodeStream(input, null, onlyBoundsOptions); input.close(); int originalWidth = onlyBoundsOptions.outWidth; int originalHeight = onlyBoundsOptions.outHeight; if ((originalWidth == -1) || (originalHeight == -1)) return null; //图片分辨率以480x800为标准 float hh = 800f;//这里设置高度为800f float ww = 480f;//这里设置宽度为480f //缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可 int be = 1;//be=1表示不缩放 if (originalWidth > originalHeight && originalWidth > ww) {//如果宽度大的话根据宽度固定大小缩放 be = (int) (originalWidth / ww); } else if (originalWidth < originalHeight && originalHeight > hh) {//如果高度高的话根据宽度固定大小缩放 be = (int) (originalHeight / hh); } if (be <= 0) be = 1; //比例压缩 BitmapFactory.Options bitmapOptions = new BitmapFactory.Options(); bitmapOptions.inSampleSize = be;//设置缩放比例 bitmapOptions.inDither = true;//optional bitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;//optional input = ac.getContentResolver().openInputStream(uri); Bitmap bitmap = BitmapFactory.decodeStream(input, null, bitmapOptions); input.close(); return compressImage(bitmap);//再进行质量压缩 }
三.获取系统资源
int indentify = getResources().getIdentifier("actionbar_bg", "drawable","android"); //注意,最后一个参数必须是“android”