Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bitmaps from sdcard #23

Open
kormateusz opened this issue Oct 22, 2012 · 2 comments
Open

Bitmaps from sdcard #23

kormateusz opened this issue Oct 22, 2012 · 2 comments

Comments

@kormateusz
Copy link

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
Copy link
Owner

thest1 commented Oct 23, 2012

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//issues/23.

@karthickkumar
Copy link

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants