thest1 / LazyList

Lazy load of images in Android
http://stackoverflow.com/questions/541966/android-how-do-i-do-a-lazy-load-of-images-in-listview/3068012#3068012
MIT License
1.2k stars 497 forks source link

Bitmaps from sdcard #23

Open kormateusz opened 11 years ago

kormateusz commented 11 years ago

Hi, I was trying to change this code to show bitmaps from sdcard instead of the internet. I've changed this part of the code:

private Bitmap getBitmap(String url) { File f=fileCache.getFile(url);

    //from SD cache
    Bitmap b = decodeFile(f);
    if(b!=null)
        return b;

    //from web
    try {
        Bitmap bitmap=null;
        URL imageUrl = new URL(url);
        HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
        conn.setConnectTimeout(30000);
        conn.setReadTimeout(30000);
        conn.setInstanceFollowRedirects(true);
        InputStream is=conn.getInputStream();
        OutputStream os = new FileOutputStream(f);
        Utils.CopyStream(is, os);
        os.close();
        bitmap = decodeFile(f);
        return bitmap;
    } catch (Throwable ex){
       ex.printStackTrace();
       if(ex instanceof OutOfMemoryError)
           memoryCache.clear();
       return null;
    }
}

to this:

private Bitmap getBitmap(String url) { File f=fileCache.getFile(url);

    //from SD cache
    Bitmap b = decodeFile(f);
    if(b!=null)
        return b;

    //from sdcard
    try {
        Bitmap bitmap=null;
        InputStream is=new FileInputStream(url);
        OutputStream os = new FileOutputStream(f);
        Utils.CopyStream(is, os);
        os.close();
        bitmap = decodeFile(f);
        return bitmap;
    } catch (Throwable ex){
       ex.printStackTrace();
       if(ex instanceof OutOfMemoryError)
           memoryCache.clear();
       return null;
    }
}

And I've used my ArrayAdapter:

public class AdapterFiles extends ArrayAdapter{ private int resource; … private String item; private ViewHolder viewHolder; … public ImageLoader imageLoader;

public AdapterFiles(Context context, int textViewResourceId, int label, List<String> objects) {
        super(context, textViewResourceId, objects);
        …
        resource = textViewResourceId;
        imageLoader=new ImageLoader(getContext());

}

static class ViewHolder {
    TextView label;
    ImageView ikonka;
    …

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    RelativeLayout RowView;
        item = getItem(position);

        if(convertView == null) {
        RowView = new RelativeLayout(getContext());
        LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(resource, RowView, true);
        viewHolder = new ViewHolder();
        viewHolder.label = (TextView)RowView.findViewById(R.id.label);
        viewHolder.ikonka = (ImageView)RowView.findViewById(R.id.icon);
        …
        RowView.setTag(viewHolder);
        } else {
            RowView = (RelativeLayout)convertView;
            viewHolder = (ViewHolder) RowView.getTag();
        }

            …

        File file = new File(item);
        if (file.isDirectory()){
            if(file.canRead()){
            viewHolder.ikonka.setImageResource(R.drawable.folder);
            }
            else{
            viewHolder.ikonka.setImageResource(R.drawable.foldernoway); 
            }
        …

        else if(item.endsWith(".jpg") || item.endsWith(".JPG") || item.endsWith(".png") || item.endsWith(".jpeg")){

            if(thumbnails == false){
            viewHolder.ikonka.setImageResource(R.drawable.image);   
            }else{

                imageLoader.DisplayImage(item, viewHolder.ikonka);

            }
            }           
        …

        viewHolder.label.setText(file.getName());

        …          

        return RowView;

}

And now, I have two problems..

  1. Images don't appear when I scroll listview down. I have to scroll down, up and again down to view images. How can I fix it?
  2. How to resize bitmaps in the cache folder?
thest1 commented 11 years ago

Unfortunately I can't tell you what's wrong. You should just debug and find where exactly it goes wrong.

Also if you have images on SD you don't need to cache them to SD once again. So you could remove everything related to SD cache.

2012/10/22 kormateusz notifications@github.com

Hi, I was trying to change this code to show bitmaps from sdcard instead of the internet. I've changed this part of the code:

private Bitmap getBitmap(String url) { File f=fileCache.getFile(url);

//from SD cache
Bitmap b = decodeFile(f);
if(b!=null)
    return b;

//from web
try {
    Bitmap bitmap=null;
    URL imageUrl = new URL(url);
    HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
    conn.setConnectTimeout(30000);
    conn.setReadTimeout(30000);
    conn.setInstanceFollowRedirects(true);
    InputStream is=conn.getInputStream();
    OutputStream os = new FileOutputStream(f);
    Utils.CopyStream(is, os);
    os.close();
    bitmap = decodeFile(f);
    return bitmap;
} catch (Throwable ex){
   ex.printStackTrace();
   if(ex instanceof OutOfMemoryError)
       memoryCache.clear();
   return null;
}

}

to this:

private Bitmap getBitmap(String url) { File f=fileCache.getFile(url);

//from SD cache
Bitmap b = decodeFile(f);
if(b!=null)
    return b;

//from sdcard
try {
    Bitmap bitmap=null;
    InputStream is=new FileInputStream(url);
    OutputStream os = new FileOutputStream(f);
    Utils.CopyStream(is, os);
    os.close();
    bitmap = decodeFile(f);
    return bitmap;
} catch (Throwable ex){
   ex.printStackTrace();
   if(ex instanceof OutOfMemoryError)
       memoryCache.clear();
   return null;
}

}

And I've used my ArrayAdapter:

public class AdapterFiles extends ArrayAdapter{ private int resource; … private String item; private ViewHolder viewHolder; … public ImageLoader imageLoader;

public AdapterFiles(Context context, int textViewResourceId, int label, List objects) { super(context, textViewResourceId, objects); … resource = textViewResourceId; imageLoader=new ImageLoader(getContext());

}

static class ViewHolder { TextView label; ImageView ikonka; …

}

@Override public View getView(int position, View convertView, ViewGroup parent) { RelativeLayout RowView; item = getItem(position);

    if(convertView == null) {
    RowView = new RelativeLayout(getContext());
    LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(resource, RowView, true);
    viewHolder = new ViewHolder();
    viewHolder.label = (TextView)RowView.findViewById(R.id.label);
    viewHolder.ikonka = (ImageView)RowView.findViewById(R.id.icon);
    …
    RowView.setTag(viewHolder);
    } else {
        RowView = (RelativeLayout)convertView;
        viewHolder = (ViewHolder) RowView.getTag();
    }

        …

    File file = new File(item);
    if (file.isDirectory()){
        if(file.canRead()){
        viewHolder.ikonka.setImageResource(R.drawable.folder);
        }
        else{
        viewHolder.ikonka.setImageResource(R.drawable.foldernoway);
        }
    …

    else if(item.endsWith(".jpg") || item.endsWith(".JPG") || item.endsWith(".png") || item.endsWith(".jpeg")){

        if(thumbnails == false){
        viewHolder.ikonka.setImageResource(R.drawable.image);
        }else{

            imageLoader.DisplayImage(item, viewHolder.ikonka);

        }
        }
    …

    viewHolder.label.setText(file.getName());

    …

    return RowView;

}

And now, I have two problems..

  1. Images don't appear when I scroll listview down. I have to scroll down, up and again down to view images. How can I fix it?
  2. How to resize bitmaps in the cache folder?

— Reply to this email directly or view it on GitHubhttps://github.com/thest1/LazyList/issues/23.

karthickkumar commented 11 years ago

@kormateusz @thest1 Yeah its working correctly as per @kormateusz change.Nothing need to change,its enough to load the image from sdcard. May be problem in adapter getview method,@kormateusz need to debug.