Welcome to XDA

Search to go directly to your device's forum

Register an account

Unlock full posting privileges

Ask a question

No registration required
Post Reply

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

OP arpitkh96

22nd April 2014, 02:51 PM   |  #1  
arpitkh96's Avatar
OP Senior Member
Flag panipat
Thanks Meter: 553
 
1,126 posts
Join Date:Joined: Feb 2012
Donate to Me
More
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
22nd April 2014, 10:33 PM   |  #2  
SimplicityApks's Avatar
Senior Member
Flag Aachen
Thanks Meter: 337
 
347 posts
Join Date:Joined: May 2013
More
Quote:
Originally Posted by arpitkh96

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: [ View ]
22nd April 2014, 10:58 PM   |  #3  
arpitkh96's Avatar
OP Senior Member
Flag panipat
Thanks Meter: 553
 
1,126 posts
Join Date:Joined: Feb 2012
Donate to Me
More
Quote:
Originally Posted by SimplicityApks

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
23rd April 2014, 08:07 AM   |  #4  
arpitkh96's Avatar
OP Senior Member
Flag panipat
Thanks Meter: 553
 
1,126 posts
Join Date:Joined: Feb 2012
Donate to Me
More
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  
23rd April 2014, 08:09 AM   |  #5  
arpitkh96's Avatar
OP Senior Member
Flag panipat
Thanks Meter: 553
 
1,126 posts
Join Date:Joined: Feb 2012
Donate to Me
More
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
Post Reply Subscribe to Thread
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes