Attend XDA's Second Annual Developer Conference, XDA:DevCon 2014!
5,781,559 Members 40,653 Now Online
XDA Developers Android and Mobile Development Forum

[Q]Problem in loading icons of apks(files) using asynctask

Tip us?
 
arpitkh96
Old
#1  
arpitkh96's Avatar
Senior Member - OP
Thanks Meter 376
Posts: 1,087
Join Date: Feb 2012
Location: panipat
Default [Q]Problem in loading icons of apks(files) using asynctask

Here are the adapter and asynctask class
Code:
class Myadapter extends ArrayAdapter<Layoutelements> {
Context context;
public Myadapter(Context context, int resourceId,List<Layoutelements> items) {
super(context, resourceId, items);
this .context = context;
}
/*private view holder class*/
private class ViewHolder {
ImageView imageView;
TextView txtTitle;
TextView txtDesc;
}
public View getView( int position, View convertView, ViewGroup parent) {
ViewHolder holder = null ;
Layoutelements rowItem = getItem(position);
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

convertView = mInflater.inflate(R.layout.rowlayout, null );
holder = new ViewHolder();
holder.txtDesc = (TextView) convertView.findViewById(R.id.secondLine);
holder.txtTitle = (TextView) convertView.findViewById(R.id.firstline);
holder.imageView = (ImageView) convertView.findViewById(R.id.icon);

holder.txtDesc.setText(rowItem.getDesc().toString());
holder.txtTitle.setText(rowItem.getTitle());
String ext=getFileExtension(rowItem.getDesc());
if(ext.equals(".apk"))
{holder.imageView.setImageDrawable(rowItem.getImageId());

holder.imageView.setTag(rowItem.getDesc());//
////tag of imageView == path
////to image
     new ImageDownloaderTask(holder.imageView).execute(rowItem.getDesc());
}
else if(ext.equals(".png") || ext.equals(".jpg") || ext.equals(".jpeg"))
{holder.imageView.setImageDrawable(rowItem.getImageId());

holder.imageView.setTag(rowItem.getDesc());//
////tag of imageView == path
////to image
new LoadImage(holder.imageView).execute(rowItem.getDesc());
}
else{
holder.imageView.setImageDrawable(rowItem.getImageId());}
return convertView;
}
}
Code:
class ImageDownloaderTask extends AsyncTask<String, Void, Drawable> {
private final WeakReference<ImageView> imageViewReference;
private String path;
public ImageDownloaderTask(ImageView imageView) {
imageViewReference = new WeakReference<ImageView>(imageView);
}

@Override
// Actual download method, run in the task thread
protected Drawable doInBackground(String... params) {
// params comes from the execute() call: params[0] is the url.
path =params[0];

try{PackageManager pm=getPackageManager();
PackageInfo    pi=pm.getPackageArchiveInfo(path,0);
//// the secret arethese two lines....
pi.applicationInfo.sourceDir=path;
pi.applicationInfo.publicSourceDir=path;
////
return pi.applicationInfo.loadIcon(pm);
}catch(Exception e){return getResources().getDrawable(R.drawable.apk);}

}

@Override
// Once the image is downloaded, associates it to the imageView
protected void onPostExecute(Drawable bitmap) {
if (isCancelled()) {
bitmap = null;
}

if (imageViewReference != null) {
ImageView imageView = imageViewReference.get();
if (imageView != null) {

if (bitmap != null) {
imageView.setImageDrawable(bitmap);
} else {
imageView.setImageDrawable(imageView.getContext().getResources()
   .getDrawable(R.drawable.apk));
}
}

}
}

}
I am making a file manager.
Now the problem is like this.
If i scroll down icons load correctly. But when i scroll up, the icons load again.they are not cached once they are loaded.i want that icons are cached till directory is changed.

Sent from my GT-S5570 using XDA Premium 4 mobile app
 
SimplicityApks
Old
#2  
SimplicityApks's Avatar
Senior Member
Thanks Meter 324
Posts: 319
Join Date: May 2013
Location: Aachen
Quote:
Originally Posted by arpitkh96 View Post
I am making a file manager.
Now the problem is like this.
If i scroll down icons load correctly. But when i scroll up, the icons load again.they are not cached once they are loaded.i want that icons are cached till directory is changed.
Try using an LruCache or your images. Also, you might want to take a look at this guide explaining it further.
The Following User Says Thank You to SimplicityApks For This Useful Post: [ Click to Expand ]
 
arpitkh96
Old
#3  
arpitkh96's Avatar
Senior Member - OP
Thanks Meter 376
Posts: 1,087
Join Date: Feb 2012
Location: panipat
Quote:
Originally Posted by SimplicityApks View Post
Try using an LruCache or your images. Also, you might want to take a look at this guide explaining it further.
Thanks i will check them

Sent from my GT-S5570 using XDA Premium 4 mobile app
 
arpitkh96
Old
#4  
arpitkh96's Avatar
Senior Member - OP
Thanks Meter 376
Posts: 1,087
Join Date: Feb 2012
Location: panipat
It worked but is buggy.the images many times get blurred after scrolling up as shown in screenshots.See the thumbnails in both images

Sent from my GT-S5570 using XDA Premium 4 mobile app
Attached Thumbnails
Click image for larger version

Name:	1398233206506.jpg
Views:	9
Size:	16.3 KB
ID:	2703250  
 
arpitkh96
Old
#5  
arpitkh96's Avatar
Senior Member - OP
Thanks Meter 376
Posts: 1,087
Join Date: Feb 2012
Location: panipat
After scrollClick image for larger version

Name:	1398233286670.jpg
Views:	11
Size:	15.5 KB
ID:	2703252
Is this due to low cache memory.should i try on another device.should i show you the new modified code.


Sent from my GT-S5570 using XDA Premium 4 mobile app
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes