android


Downloading Image from server using universal image loader


In my app I'm downloading Images from server and setting images into view pager.Everything works fine but there is a slight delay(say 8-10 sec on Wifi) in downloading image. The image size ranges from 200kb-800kb. and I have also added a progress bar while downloading is in progress but the progress bar does not shows up.
Can somebody help me on this.
Below is the code.
public void fetchImageUrl() {
progressBar.setVisibility(View.VISIBLE);
String url = URLMap.getUploadImgChangeStatus("get_amazonserver_image");
url = url.replace("{id}", companyId).replace("{crid}", callRecordId);
StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
System.out.println("Response0000" + response);
if (response.equals("[]")) {
setView();
} else {
getAmazonImageUrl(response);
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.i("ErrorListner=", error.getMessage());
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(request);
}
public void getAmazonImageUrl(String response) {
try {
imgUrl = new ArrayList<>();
String[] arr;
JSONArray jArray = new JSONArray(response);
for (int i = 0; i < jArray.length(); i++) {
if (jArray.getJSONObject(i).has("ImagePath")) {
arr = jArray.getJSONObject(i).getString("ImagePath").split("\\s+");
imgUrl = Arrays.asList(arr);
}
}
} catch (JSONException e) {
}
imageViewAdapter = new ImageViewAdapter(this, iv, imgUrl, viewPager, this);
viewPager.setAdapter(imageViewAdapter);
progressBar.setVisibility(View.GONE);
}
Below is my adapter class
public class ImageViewAdapter extends android.support.v4.view.PagerAdapter {
private Context context;
ImageView imageView;
ProgressBar progressBar;
private LayoutInflater layoutInflater;
ViewPager viewPager;
Activity activity;
List<String> imgUrl;
public ImageViewAdapter(Context context, ImageView imageView, List<String> imgUrl,
ViewPager viewPager, Activity activity){
System.out.println("Inside ImageViewAdapter");
this.context=context;
this.imageView = imageView;
this.viewPager = viewPager;
this.activity = activity;
this.imgUrl=imgUrl;
System.out.println(imgUrl.size());
}
#Override
public int getCount() {
return imgUrl.size();
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((LinearLayout) object);
}
#Override
public Object instantiateItem(ViewGroup container, final int position) {
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View item_view = layoutInflater.inflate(R.layout.image_swipe_custom_layout, container, false);
//setting custom font
Typeface custom_font = Typeface.createFromAsset(context.getAssets(),"fonts/DroidSerif.ttf");
TextView tv = (TextView) item_view.findViewById(R.id.image_count);
tv.setTypeface(custom_font);
imageView = (ImageView) item_view.findViewById(R.id.imageView1);
progressBar=(ProgressBar)item_view.findViewById(R.id.imgLoadingPbar);
// UNIVERSAL IMAGE LOADER SETUP
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
.cacheOnDisc(true).cacheInMemory(true)
.imageScaleType(ImageScaleType.EXACTLY)
.displayer(new FadeInBitmapDisplayer(300)).build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
context)
.defaultDisplayImageOptions(defaultOptions)
.memoryCache(new WeakMemoryCache())
.discCacheSize(100 * 1024 * 1024).build();
com.nostra13.universalimageloader.core.ImageLoader.getInstance().init(config);
// UNIVERSAL IMAGE LOADER OPTION SETUP
DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true)
.cacheOnDisc(true).resetViewBeforeLoading(true)
.showImageForEmptyUri(R.drawable.ic_user)
.showImageOnFail(R.drawable.noimgavailable)
.showImageOnLoading(R.drawable.spinner_border).build();
/*final Animation animation= AnimationUtils.loadAnimation(context,R.anim.rotate);*/
com.nostra13.universalimageloader.core.ImageLoader imageLoader = com.nostra13.universalimageloader.core.ImageLoader.getInstance();
imageLoader.displayImage(imgUrl.get(position), imageView, options, new ImageLoadingListener() {
#Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
progressBar.setVisibility(View.GONE);
}
#Override
public void onLoadingStarted(String imageUri, View view) {
progressBar.setProgress(0);
progressBar.setVisibility(View.VISIBLE);
}
#Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
progressBar.setVisibility(View.GONE);
}
#Override
public void onLoadingCancelled(String imageUri, View view) {
}
});
if(position == 0) {
tv.setText("Image " + "1");
tv.setTypeface(custom_font);
}else if(position == 1) {
tv.setText("Image " + "2");
tv.setTypeface(custom_font);
} else if(position == 2) {
tv.setText("Image " + "3");
tv.setTypeface(custom_font);
}
container.addView(item_view);
return item_view;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
//((ViewPager)container).removeView((View)object);
container.removeView((LinearLayout) object);
}
}
I have tried using picasso, Async task but everything is same.. Picasso and universal image loader almost takes same time but async task is even more late.
The progress bar which is set in activity class shows up for a second or two second. but when it goes to adapter class I have another progress bar which does not shows up.
My problems are
1. Is there any way where I can download images faster
2. and how make use of progress bar properly.
Thanks in advance
Ok I got the solution. along with progress bar we need to set view as well.
I'm posting code.
com.nostra13.universalimageloader.core.ImageLoader imageLoader = com.nostra13.universalimageloader.core.ImageLoader.getInstance();
imageLoader.displayImage(imgUrl.get(position), imageView, options, new ImageLoadingListener() {
#Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
progressBar.setVisibility(View.GONE);
}
#Override
public void onLoadingStarted(String imageUri, View view) {
//progressBar.setProgress(0);
progressBar.setVisibility(View.VISIBLE);
view.setVisibility(View.GONE);
}
#Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
progressBar.setVisibility(View.GONE);
view.setVisibility(View.VISIBLE);
}
#Override
public void onLoadingCancelled(String imageUri, View view) {
}
});
You can add the Progress Bar in specific position.Sample code are belowe. If there is any problem follow this link Universal Image Loader description
imageLoader.displayImage(imageUri, imageView, options, new ImageLoadingListener() {
#Override
public void onLoadingStarted(String imageUri, View view) {
...
}
#Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
...
}
#Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
...
}
#Override
public void onLoadingCancelled(String imageUri, View view) {
...
}
}, new ImageLoadingProgressListener() {
#Override
public void onProgressUpdate(String imageUri, View view, int current, int total) {
...
}
});

Related Links

What kind of Button can toggle/change states in Android?
How do you translate your Android app? [closed]
How do i pass Uri of image clicked in custom gallery to another activity?
Getting onActivityResult in Audio Intent
Android GPS false location retirieved
How do I change the phone number of an android emulator
With just one particular Bluetooth SPP module, .createRfcommSocketToServiceRecord() suddenly no longer works, but reflection method works
'Total Installs' in Android
Mobile version of Applications I need, to let App visible on Mobiles [closed]
ndk-build eclipse argument: not find class
How to stop freeze listView
Downloading data: every time or once?
Orientation Change - Background
Eclipse not seeing android device
Adding icon on arraylist
How to programmatically adjust z order of Views

Categories

HOME
shell
facebook-oauth
twitter-bootstrap-3
nvd3.js
rsa
youtube-data-api-v3
yahoo-finance
hessian
jelastic
package
grafana
replace
newrelic
rom
babeljs
code-coverage
mvc5
trac
odoo-8
accessibility
sendgrid-api-v3
augeas
gource
azure-iot-hub
alpacajs
package.json
chargify
symbols
ag
poedit
articulate-storyline
parcelable
php-mysqlidb
exit
onload
tpm
paperjs
browser-sync
jaws
powershell-ise
contacts
hivemq
xcode7.3
odroid
apns-php
opensmpp
setup.py
flask-login
plan-9
servermanager
mesosphere
httrack
term
chai-as-promised
tf
bluez
google-cse
magic-draw
video-conferencing
modelsim
subnet
headless
ngmaterial
sundials
rocks
chromium-os
slackware
embedding
cda
uiactionsheet
cortex-m
cycle2
state-restoration
r-package
ess
include-guards
ons-api
binomial-theorem
cgi-bin
canalyzer
rpart
finalbuilder
typeconverter
openbabel
rapidsvn
showdialog
theos
genymotion-call
named-parameters
codeplex
wxformbuilder
buffering
websphere-esb
artemis
http-patch
inputmismatchexception
hidapi
cnf
merb
twisted.web
sqlclr
subgraph
urlrewriter
htdocs
winmain
cgbitmapcontextcreate
msr
ie-compatibility-mode
burndowncharts
web-application-project
tui
sun
wcf-proxy

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App