-4

Please tell me how I can do smooth scrolling. When I try to scroll down it takes a lot of time to load videos thumbnails. Please check my code and give me a easy solution. Many thanks.

I have tried a lot of ways but not working. Don't know how to deal with this issue. I hope experts will help me to complete my project.

public class VideoAdapter extends BaseAdapter {
    private Context vContext;

    public VideoAdapter(Context c) {
        vContext = c;
    }

    public int getCount() {
        return count;
    }

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

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

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


            System.gc();
                ViewHolder holder;
                String id = null;
                convertView = null;

            if (convertView == null) {
                try {
                convertView = LayoutInflater.from(vContext).inflate(
                        R.layout.itemlist, parent, false);
                }
                catch (Exception e)
                {
                    Toast.makeText(getContext(),"Permission Granted" + e, Toast.LENGTH_SHORT).show();
                }

                    holder = new ViewHolder();
                try {
                    holder.txtTitle = (TextView) convertView
                            .findViewById(R.id.txtTitle);
                    holder.txtSize = (TextView) convertView
                            .findViewById(R.id.txtSize);
                    holder.thumbImage = (ImageView) convertView
                            .findViewById(R.id.imgIcon);
                }
                catch (Exception e)
            {
                Toast.makeText(getContext(),"Permission Granted" + e, Toast.LENGTH_SHORT).show();
            }
try {
    video_column_index = songCursor
            .getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME);
    songCursor.moveToPosition(position);
    id = songCursor.getString(video_column_index);
    video_column_index = songCursor
            .getColumnIndexOrThrow(MediaStore.Video.Media.SIZE);
    songCursor.moveToPosition(position);
} catch (Exception e)
{
    Toast.makeText(getContext(),"Permission Granted" + e, Toast.LENGTH_SHORT).show();

}
                // id += " Size(KB):" +
                // videocursor.getString(video_column_index);
                holder.txtTitle.setText(id);
                holder.txtSize.setText(" Size(KB):"
                        + songCursor.getString(video_column_index));
try {
    String[] proj = {MediaStore.Video.Media._ID,
            MediaStore.Video.Media.DISPLAY_NAME,
            MediaStore.Video.Media.DATA};

    @SuppressWarnings("deprecation")
    Cursor cursor = getActivity().getContentResolver().query(
            MediaStore.Video.Media.EXTERNAL_CONTENT_URI, proj,
            MediaStore.Video.Media.DISPLAY_NAME + "=?",
            new String[]{id}, null);
    cursor.moveToFirst();

}
catch (Exception e)
{
    Toast.makeText(getContext(),"nO Permission Granted" + e, Toast.LENGTH_SHORT).show();

}

    long ids = songCursor.getLong(songCursor
            .getColumnIndex(MediaStore.Video.Media._ID));

    ContentResolver crThumb = getActivity().getContentResolver();
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 1;
    Bitmap curThumb = MediaStore.Video.Thumbnails.getThumbnail(crThumb, ids, MediaStore.Video.Thumbnails.MICRO_KIND, options);
           ///     if (holder.thumbImage !=null) {
                    holder.thumbImage.setImageBitmap(curThumb);
              //  }
          //      else {
          //          Drawable a = getContext().getResources().getDrawable(R.drawable.index);
           ///         holder.thumbImage.setImageDrawable(a);

              //  }
     curThumb = null;
}


            return convertView;

         }

        }

    static class ViewHolder {

        TextView txtTitle;
        TextView txtSize;
        ImageView thumbImage;
    }
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
ALI
  • 23
  • 1
  • 6
  • 1
    can you read anything in such formatted code? – pskink Sep 11 '17 at 05:23
  • The problem is caused by you doing the retrieve images in the main thread.Try to create a new thread and do the operation. – Sharath kumar Sep 11 '17 at 05:29
  • 1) use recycler view instead on list 2) check whether you are adding listview/recycler view insider scrollview. adding list inside scrollview causes such problem 3) hope you are using libraries like picasso or glide to load images – Vishal Sep 11 '17 at 06:44

2 Answers2

1

I had the same problem. The storage is slow.

You have to lazy load the images. To accomplish this, I created a custom ImageView.

public void setImageLazy(Uri image) {
    new Thread(new Runnable() {
        Bitmap bitmap = get your image or default ;
        runOnUiThread(new Runnable() {
            setImage(bitmap);
        }
    }.start();
}

If it uses to much resources use a looper thread and queue the runnables.

Edit: This attempt works but is not good. Consider using a HandlerThread instead.

raldone01
  • 405
  • 4
  • 14
1

Don't do all code in getView() method, because it runs everytime you scroll list, this is the cause of slow scrolling, You don't even need to fetch bitmaps of all videos it may generate OutOfMemoryException, fetch all paths of videos in fragment or activity then pass list of paths to the constructor of adapter, then use Glide library to show thumbnail of video using path: Put this line in App Gradle

compile 'com.github.bumptech.glide:glide:4.1.1'

then write this code to show thumbnail of video:

Glide.with(vContext).load(paths[position]).into(holder.thumbImage);

You can get the path from cursor:

protected String[] getPaths() {
    Cursor cursor = getActivity().getContentResolver()
            .query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection,
                    null, null, null);
    if (cursor == null)
        return null;

    String[] paths = new String[cursor.getCount()];
    File file;

    int i = 0;
    if (cursor.moveToFirst()) {
        do {
            String path = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.DATA));
                /*
                It may happen that some image file paths are still present in cache,
                though image file does not exist. These last as long as media
                scanner is not run again. To avoid get such image file paths, check
                if image file exists.
                 */
            file = new File(path);

            if (file.exists()) {
                paths[i] = path;
                i++;
            }

        } while (cursor.moveToNext());
    }
    cursor.close();
    return paths;
}
Divy Soni
  • 824
  • 9
  • 22