1

Here I am getting stored picture paths from the database. So that I am showing the images in the gridview with the help of paths. But as it is shown in my code below, I am loading cursoradapter in the asynctask. The problem here is for the first time it is okay that it takes some time until all images are loaded.

But if user adds one more pic to the database, it again takes much time to load all images as I kept the asynctask in onResume() to make the added picture also visible. The same is the case whenever user adds each picture. Can someone suggest me a simple way to get the last added picture visible in the gridview without having to load all the pictures again. Or in the least case can some one help me implementing lazy adapter to the present cursoradapter if that is not possible?

    public class ImagesScreen extends Activity {
    public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.images_screen);
                    gridView = (GridView)findViewById(R.id.gridView1);
            String[] from = new String[] { Reminders.MOM_PATHS };
            int[] to = new int[] {};
            dbCon = new SQLiteConnectClass(ImagesScreen.this);

            imageCursorAdapter = new ImageCursorAdapter(ImagesScreen.this, R.layout.griditem, null, from, to);
            gridView.setAdapter(imageCursorAdapter);

        }

             protected void onResume() {
            super.onResume();
            new GetImages().execute((Object[]) null);
        }

       private class GetImages extends AsyncTask<Object, Object, Cursor> {

            @Override
            protected Cursor doInBackground(Object... params) {
                return dbCon.getAllImages();
            }

            @Override
            protected void onPostExecute(Cursor result) {
                imageAdapter.changeCursor(result);  
            }

public void addpictures(View view){
     // code for adding picture paths to the database by transferring to another activity.
}


        }

CustomAdapter class:

public class ImageCursorAdapter extends SimpleCursorAdapter{

        private int layout;
        private LayoutInflater mLayoutInflater;
        private Context mContext;

        public ImageAdapter(Context context, int layout, Cursor c,String[] from, int[] to) {
            super(context, layout, c, from, to,0);
            this.layout = layout;
            mLayoutInflater=LayoutInflater.from(context);
            mContext = context;
        }

        public View newView(Context context, Cursor cursor, ViewGroup parent) {     
            View v = mLayoutInflater.inflate(layout, parent, false);
            return v;
        }

        public void bindView(final View v, final Context context, Cursor c) {
            final int id = c.getInt(c.getColumnIndex("id"));
            final String imagepath = c.getString(c.getColumnIndex("paths"));


            ImageView imageview = (ImageView)v.findViewById(R.id.imageView1);

            File imgFile = new File(imagepath);
            if(imgFile.exists()){

                Bitmap imageBitmap = decodeFile(imgFile);
                imageview.setImageBitmap(imageBitmap);
            }

        }
}
Apparatus
  • 411
  • 1
  • 5
  • 19

2 Answers2

4

use this code

public class Imagegallery extends Activity implements OnItemClickListener,
    OnScrollListener, OnTouchListener {
Button btn;

SQLiteDatabase sampleDB = null;
String SAMPLE_DB_NAME = "hic";

int size = 0;
TextView headertext;
Button cartbtn;

ImageView footer1, footer2, footer3, footer4, footer5;

GridView gridView;

boolean larg = false;
String products;
int j = 1;
int ii = 0;
String imagepath[];
String productname[];
int productid[] = new int[1000];

String productids[] = new String[1000];
int integerprodids[] = new int[1000];
final Context context = this;
String filename2[];

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.currentspecial);

    File root = new File(Environment.getExternalStorageDirectory()
            + File.separator + "aiwhic/product" + File.separator);
    File[] fileName = root.listFiles();
    filename2 = new String[fileName.length];
    for (int j = 0; j < fileName.length; j++) {
        Uri uri = Uri.fromFile(fileName[j]);
        filename2[j] = fileName[j].getAbsolutePath();

        Log.e("file", filename2[j]);
    }

    gridView = (GridView) findViewById(R.id.gridView1);

    gridView.setAdapter(new ImageAdapter(this, filename2, filename2));

    gridView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {

            int isf = filename2[position].lastIndexOf("/");
            int laf = filename2[position].lastIndexOf(".");
            String filename = filename2[position].substring(isf + 1, laf);
            int pos = Integer.parseInt(filename);
            Intent go = new Intent(Imagegallery.this, ItemsPage.class);
            Bundle bundle = new Bundle();

            bundle.putInt("position", pos);
            bundle.putString("whichclass", "imagegallery");
            bundle.putInt("catid", 2);
            go.putExtras(bundle);
            startActivity(go);

        }
    });

}

public class ImageAdapter extends BaseAdapter {
    private Context context;
    private final String[] mobileValues;
    private final String[] mobileimages;

    public ImageAdapter(Context context, String[] mobileValues, String[] mo) {
        this.context = context;
        this.mobileValues = mobileValues;
        this.mobileimages = mo;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View gridView;

        if (convertView == null) {

            gridView = new View(context);

            gridView = inflater.inflate(R.layout.currentspeciallist, null);

            TextView textView = (TextView) gridView
                    .findViewById(R.id.textView1);
            textView.setText(mobileValues[position]);
            textView.setVisibility(View.INVISIBLE);
            ImageView imageView = (ImageView) gridView
                    .findViewById(R.id.imageView1);

            imageView.setTag(mobileimages[position]);
            new Loadimage().execute(imageView);
            // imageView.setImageBitmap(BitmapFactory
            // .decodeFile(mobileimages[position]));

        } else {
            gridView = (View) convertView;
        }

        return gridView;
    }

    public int getCount() {
        return mobileimages.length;
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

}

class Loadimage extends AsyncTask<Object, Void, Bitmap> {

    private ImageView imv;
    private String path;

    @Override
    protected Bitmap doInBackground(Object... params) {

        imv = (ImageView) params[0];

        path = imv.getTag().toString();

        // Bitmap thumb = BitmapFactory.decodeFile(path);
        BitmapFactory.Options opts = new BitmapFactory.Options();

        opts.inSampleSize = 2; // for 1/2 the image to be loaded
        Bitmap thumb = Bitmap.createScaledBitmap(
                BitmapFactory.decodeFile(path, opts), 120, 120, false);

        return thumb;

    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        if (!imv.getTag().toString().equals(path)) {
            /*
             * The path is not same. This means that this image view is
             * handled by some other async task. We don't do anything and
             * return.
             */
            return;
        }

        if (bitmap != null && imv != null) {
            imv.setVisibility(View.VISIBLE);
            imv.setImageBitmap(bitmap);
        } else {
            imv.setVisibility(View.GONE);
        }
    }

}

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

}

then create a xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="5dp" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/ic_launcher" >
    </ImageView>
</RelativeLayout>

i am getting file path directly . u just get the name from sqlite and pass the array string

WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
Senthil
  • 1,244
  • 10
  • 25
  • +1 Your effort is appreciated. But your code also uses asynctask just like mine which doesn't make any difference. It doesn't fetch images unless all images are finished downloading – Apparatus Apr 06 '13 at 09:00
0

use Android-Universal-Image-Loader and set

ImageView imageview = (ImageView)v.findViewById(R.id.imageView1);

     File imgFile = new File(imagepath);
         if(imgFile.exists()){
               imageLoader.displayImage(imageUri(ur file path), imageView);
                }
kyogs
  • 6,766
  • 1
  • 34
  • 50
  • Thanks. Can you please tell me which parts of code need to be used in that library to make use of this method displayimage(). Because there are many? – Apparatus Apr 06 '13 at 09:02