Question

I'm trying to build a list with an image that is taken from the device and a text. It turns out that taking images from the phone that was from the phone's camera is a task that takes a while so I'm trying to make it as fast as possible so the user experience won't get slower. All I got from this is that it looks like all the images are loaded in one ImageView and than the images spread to all the other ImageViews (I'm not completely sure that my implementation of the ViewHolder technique and Custom CursorAdapter is correct).

public class MyCustomCurserAdapter extends CursorAdapter {
  static class ViewHolder {
    public TextView nameText;
    public ImageView imageThumbnail;
  }


  Cursor cursor;

  public MyCustomCurserAdapter(Context context, Cursor c, int flags) {
    super(context, c, flags);
    // TODO Auto-generated constructor stub
  }

  @Override
  public void bindView(View view, Context arg1, Cursor cursor) {

    ViewHolder holder = (ViewHolder)view.getTag();


    int pathCol = cursor.getColumnIndex(NewPicSQLiteHelper.COLUMN_PATH);
    String imageInSD = cursor.getString(pathCol);  
    File imgFile = new  File(imageInSD);

    if(imgFile.exists()){

        int nameCol = cursor.getColumnIndex(NewPicSQLiteHelper.COLUMN_PIC_NAME);
        String name = cursor.getString(nameCol);

        if (name != null) 
            holder.nameText.setText(name);

        ImageTask task = new ImageTask(holder.imageThumbnail);
        task.execute(imgFile);
    }

  }

  @Override
  public View newView(Context arg0, Cursor cur, ViewGroup parent) {
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    View view = inflater.inflate(R.layout.new_pic_item, parent, false);
    ViewHolder holder = new ViewHolder();

    holder = new ViewHolder();
    holder.nameText = (TextView) view.findViewById(R.id.pic_name_entry);
    holder.imageThumbnail = (ImageView) view.findViewById(R.id.pic_thumbnail);

    // The tag can be any Object, this just happens to be the ViewHolder
    view.setTag(holder);


    return view;
  }


  private class ImageTask extends AsyncTask<File, Void, Bitmap>{
    private final WeakReference <ImageView> imageViewReference;

    public ImageTask(ImageView imageView) {
        imageViewReference = new WeakReference <ImageView> (imageView);
    }

    @Override
    protected Bitmap doInBackground(File... params) {
        String path = params[0].getAbsolutePath();

        return decodeSampledBitmapFromResource(path,75,75);
    }

    @Override
    protected void onPostExecute(Bitmap result) {
        if (imageViewReference != null) {
            ImageView imageView = imageViewReference.get();
            if (imageView != null) {
                if (result != null) {
                    imageView.setImageBitmap(result);
                    imageView.setVisibility(ImageView.VISIBLE);
                } else {
                                    imageView.setVisibility(ImageView.INVISIBLE);
                }
            }

        }

    }

    private Bitmap decodeSampledBitmapFromResource(String orgImagePath, int reqWidth, int reqHeight) {


    }

    private int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {

  }
}
Was it helpful?

Solution

I think probable reason thats its taking time is because the images would be of at least 1 mb size further you can change to thumbnail and retrieve it and also if still taking time you could put lazy downloading which is done when we take image from the server(Basically what it does is it loads the text and shows image when we get the image)

OTHER TIPS

Remove ImageTask AsyncTask ..

Use a Libraries like Glide or Picasso . Very effective for almost any case where you need to fetch, resize, and display a remote image.

I used a glide for loading images from phone storage uri

Use any of the above and see the difference

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top