獲取Drawable目錄下的資源

獲取Drawable目錄下的資源

/**
 * 通過文件名獲取Drawable目錄下的圖片資源
 *
 * @param context 上下文對象
 * @param name 文件名
 * @return bitmap
 */
public static Bitmap getDrableImage(Context context, String name) {
    ApplicationInfo info = context.getApplicationInfo();
    Resources resources = context.getResources();
    int resId = resources.getIdentifier(name, "drawable", info.packageName);
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 2;
    return BitmapFactory.decodeResource(resources, resId, options);
}

/**
 * 獲取drawable目錄下的圖片Uri
 *
 * @param name 文件名
 * @return 文件對應的uri
 */
public static Uri getImageUri(Context context, String name) {
    return Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + context.getPackageName() + "/drawable/" + name);
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章