Skip to content Skip to sidebar Skip to footer

Android Custom Simplecursoradapter With Image From File With Path In Database

Gist: custom adapter gets file resource indirectly via filepath in database. Inefficiency / memory concerns. Your opinion requested. References to all searches, related links, and

Solution 1:

Two things you can do:

1) Use the ViewHolder pattern, cache the LayoutInfalter and most important: don't bind data twice:

/* ... imports */importstatic android.media.ThumbnailUtils.extractThumbnail;

publicclassTextAndImageCursorAdapterextendsSimpleCursorAdapter { 

    private LayoutInflater mLayoutInflater;
    private Context context;
    privateint layout;


    privateclassViewHolder {
        TextView textView;
        ImageView imageView;

        ViewHolder(View v) {
            textView = (TextView) v.findViewById(R.id.gui_text);
            imageView = (ImageView) v.findViewById(R.id.gui_image);
        }
    }

    publicTextAndImageCursorAdapter(Context ctx, int layout, Cursor c, String[] from, int[] to) {
        super(ctx, layout, c, from, to);
        this.context = ctx;
        this.layout = layout;
        mLayoutInflater = LayoutInflater.from(ctx);
    }


    @Overridepublic View newView(Context ctx, Cursor cursor, ViewGroup parent) {
        ViewvView= mLayoutInflater.inflate(layout, parent, false);
        vView.setTag( newViewHolder(vView) );
        // no need to bind data here. you do in laterreturn vView;// **EDITED:**need to return the view
    }

    @OverridepublicvoidbindView(View v, Context ctx, Cursor c) {
        // you might want to cache these toointiCol_Text= c.getColumnIndex(DBCOL_TEXT);
        intiCol_Image= c.getColumnIndex(DBCOL_IMAGE);

        StringsText= c.getString(iCol_Text);
        StringsFileAndPath_Image= c.getString (iCol_Image);  //// path & fileViewHoldervh= (ViewHolder) v.getTag();

        vh.textView.setText(sSomeText);
        vh.imageView.setImageBitmap ( mySetImage ( sFileAndPath_Image ) );
    }
}

2) This is really important: don't create a thumbnail on every bind. you need to cache the result:

privatevoidsetThumbnail(String path, Bitmap b) {
    // save thumbnail to some kind of cache// see comment below
}

private Bitmap getThumbnail(String path) {
    Bitmapthumbnail=null;
    // try to fetch the thumbnail from some kind of cache// see comment belowreturn thumbnail;
}

protected Bitmap mySetImage( String path ) {
    intwidth=60; intheight=40 ;

    Bitmapthumbnail= getThumbnail(path); // try to fetch thumbnailif (thumbnail != null) return thumbnail;

    FileimgFile=newFile ( path );  //// usually like: /sdcard/wherever/filename1234.bmpBitmapmyBitmap=null;

    if( imgFile.exists() ) {
            myBitmap = BitmapFactory.decodeFile ( imgFile.getAbsolutePath () );                  
    } else {
            Log.d ("oops", "no image file ... using default.");
            myBitmap = getTheDefaultImage ();  //// not shown - this is arbitrary
    }

    imgFile.close();
    thumbnail = extractThumbnail ( myBitmap, width, height );
    myBitmap.recycle();
    setThumbnail(path, thumbnail); // save thumbnail for later reusereturn thumbnail;
}     

Depending on you use case, you want to fill getThumbnail() and setThumbnail() with some kind of LruCache:

EDIT :

@Overridepublic View newView(Context ctx, Cursor cursor, ViewGroup parent) {
    ViewvView= mLayoutInflater.inflate(layout, parent, false);
    vView.setTag( newViewHolder(vView) );
    // no need to bind data here. you do in laterreturn vView;// **EDITED:**need to return the view
}

Post a Comment for "Android Custom Simplecursoradapter With Image From File With Path In Database"