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 will be the value of TAG?
What is the difference between MeasureSpec.getSize(widthMeasureSpec) and getWidth()?
Paths on canvas: Small gap between oblique lines or straight lines not on one level? (FILL or FILL_AND-STROKE)
App not working in all devices
ListView ContextAction not following layout
Android Facebook sdk 4.0 : Null exception while getting facebook user information?
Get eNb adn SNR in LTE network
Android Facebook SDK Login work only for my account
My Imageview Stops moving after scaling on Max level
how to prevent my app for video recording [closed]
Cordova location permission triggers onResume every time
Get value of selected radio button which is saved in Firebase, and set it as default state in user profile
file is encrypted or is not a database: , while compiling: select count(*) from sqlite_master;
Android - Video Image is cut from the right side in GLSurfaceView
Find all #Suppress annotation and //noinspection comment that the Lint tool of Android Studio generated before
bmptoMat (OpenCV) doesn't seem to do anything

Categories

HOME
cygwin
facebook-oauth
developer-tools
ng-idle
flex
mstest
graphics
tivoli
scripting
mips
pyqt4
gembox-spreadsheet
echo
nuget-package
odoo-8
opengl-3
google-schemas
little-proxy
mel
java.util.logging
spring-social
onedrive-api
dynatrace
do-while
screen-readers
fileserver
forgot-password
mapstruct
charles
simpleitk
undertow
react-toolbox
exploit
flask-sqlalchemy
custom-post-type
underscore.js
soot
simplecv
sql-server-ce
protein-database
apns-php
xv6
flask-login
textsum
libgphoto2
chai-as-promised
mschart
qtranslate
tinkerpop
context-sensitive-grammar
avx
lsyncd
gameplay-kit
rsa-archer-grc
cudd
async.js
burrows-wheeler-transform
uicolor
appscale
google-query-language
mediametadataretriever
cda
ikiwiki
invoke-sqlcmd
mobiledoc-kit
gocql
fiware-monitoring
zen-cart
coda
diff3
openbabel
ssis-data-flow
machine-instruction
grails-domain-class
malformedurlexception
paste
decoupling
multiautocompletetextview
xmltodict
scriptaculous
eclipse-kepler
http-status-code-410
resource-management
robotlegs
zope.interface
pyunit
subproject
jquery-address
denied
htdocs
automationelement
xui
unattended-processing
commerceserver2007
maven-1
hbm2java
application-verifier
pagemethods
lalr
sifr3
fdf
online-storage
netbeans6.1

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