/**
* 서버에서 이미지 데이터를 가져와 비트맵으로 반환
*
* @param context
* @param imageName
* @param iv
* @param baseResource
* @return
*/
@SuppressWarnings("deprecation")
public static void getBitmapFromServer(Context context, String downloadUrl, String imageName, final ImageView iv, int baseResource, SimpleImageLoadingListener listener) {
if (!TextUtils.isEmpty(imageName)) {
iv.setVisibility(View.VISIBLE);
ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.init(ImageLoaderConfiguration.createDefault(context));
DisplayImageOptions options =
new DisplayImageOptions.Builder().showImageOnLoading(baseResource).showImageForEmptyUri(baseResource).showImageOnFail(baseResource).cacheInMemory(true)
.resetViewBeforeLoading(true).cacheOnDisc(true).build();
String downloadPath = WeinaHttpConn.GET_DOWNLOAD_URL + imageName;
// Load image, decode it to Bitmap and return Bitmap to callback
imageLoader.loadImage(downloadPath, options, listener);
} else {
iv.setVisibility(View.GONE);
}
}
사용예)
private void searchBeautyTipsAPI(int index) {
String email = SharedPref.getString(context, SharedPref.USER_EMAIL);
final int idx = index;
WeinaHttpConn.searchBeautyTipsAPI(WeinaHttpConn.SEARCH_BEAUTY_TIPS, email, idx, new JsonHttpResponseHandler() {
@Override
public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
super.onFailure(statusCode, headers, throwable, errorResponse);
}
@Override
public void onStart() {
// loadingLayout.setVisibility(View.VISIBLE);
}
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject obj) {
if(obj != JSONObject.NULL) {
try {
String code = "";
JSONObject extraObj = null;
boolean hasCode = obj.has(Define.CODE);
boolean hasExtra = obj.has(Define.EXTRA);
if(hasCode) {
code = obj.getString(Define.CODE);
}
if(hasExtra) {
extraObj = obj.getJSONObject(Define.EXTRA);
}
if("0000".equals(code)) {
if(extraObj != JSONObject.NULL) {
String bannerImgName = ""; // 배너 이미지
boolean hasBannerImg = extraObj.has("image");
if(hasBannerImg) {
bannerImgName = extraObj.getString("image");
}
CommonUtils.getBitmapFromServer(context, WeinaHttpConn.GET_DOWNLOAD_URL, bannerImgName, bannerImg, R.drawable.tips_banner_sample, new SimpleImageLoadingListener() {
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
// Do whatever you want with Bitmap
bannerImg.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
int width = bannerImg.getMeasuredWidth();
int height = bannerImg.getMeasuredHeight();
if (loadedImage != null) {
loadedImage = Bitmap.createScaledBitmap(loadedImage, width, height, true);
loadedImage = CommonUtils.setRoundCornerBitmap(loadedImage, 21);
bannerImg.setScaleType(ScaleType.FIT_XY);
bannerImg.setImageBitmap(loadedImage);
}
};
});
}
} else if("9001".equals(code)) { // 파라메터 값 전달 오류
Log.e(TAG, getResources().getString(R.string.error_info_9001));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
// loadingLayout.setVisibility(View.GONE);
}
});
}
'프로그래밍 > Android' 카테고리의 다른 글
Android getCountryCode (0) | 2015.03.16 |
---|---|
Android 기기별 해상도 (0) | 2015.03.10 |
Android TextView 기본 여백 없애기 (0) | 2015.02.26 |
Android getRoundedBitmapFromServer (0) | 2015.02.25 |
Android ViewPager PageAdapter notifyDataSetChanged (0) | 2015.02.25 |
댓글