0

I've been working on making a video gallery for myself and got stuck here. Followed this link for some references but still getting some problems:

For making thumbnails for the videos

Here is my code :

public class AddFragment extends Fragment {

private ImageButton imageButton;
private GridView gridView;

private File files;

ArrayList<File> list;

public AddFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_add, container, false);
    imageButton = (ImageButton) view.findViewById(R.id.gotoButton);
    gridView = (GridView) view.findViewById(R.id.grid_view);
    gridView.setAdapter(new ImageAdapter(getContext()));
    list = videoReader(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES));


    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            //for making the button visible as ssonas the item gets selected
            imageButton.setVisibility(view.VISIBLE);

        }
    });

    return view;
}

ArrayList<File> videoReader(File root) {

    ArrayList<File> arrayList = new ArrayList<>();

    File[] file = root.listFiles();

    for(int i=0;i<file.length;i++){
        if(file[i].isDirectory()){

        }else{
            if(file[i].getName().endsWith(".mp4")){
                arrayList.add(file[i]);
            }
        }
    }

   return arrayList;
}


public class ImageAdapter extends BaseAdapter{

    private Bitmap bitmap;

    private final Context context;

    private ImageAdapter(Context c){
            context = c;
    }

    //for the video numbers
    @Override
    public int getCount() {
        return list.size();
    }

   //for getting the video items position vise
    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {

        ImageView picturesView;
        if (convertView == null) {
            picturesView = new ImageView(context);
            if(list.get(position).contains(".jpg"))
            {
                bitmap = BitmapFactory.decodeFile(list.get(position)); //Creation of Thumbnail of image
            }
            else if(list.get(position).contains(".mp4"))
            {
                bitmap = ThumbnailUtils.createVideoThumbnail(list.get(position), 0); //Creation of Thumbnail of video
            }
            picturesView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            picturesView.setPadding(8, 8, 8, 8);
            picturesView.setLayoutParams(new GridView.LayoutParams(100, 100));
        }
        else
        {
            picturesView = (ImageView)convertView;
        }
        return picturesView;
    }
}  }

The problems I'm getting are in my ImageAdapter class in getView method

These are :

1. In the if(list.get(position).contains(".jpg")) //cannot resolve contains

2. In bitmap = BitmapFactory.decodeFile(list.get(position)); //saying the decodeFile(java.lang.string) from Bitmapfacotory cannot be applied to (java.file.io)

P.S. for the second option I tried doing that after getting reference from this link but failed:

Java contradicting behavior resolved

halfer
  • 19,824
  • 17
  • 99
  • 186
Alok
  • 8,452
  • 13
  • 55
  • 93

2 Answers2

1

Try this.

if(list.get(position).contains(".jpg"))
{
     bitmap = BitmapFactory.decodeFile(list.get(position).toString()); 
}

list.get(position) is File object and you need to pass String object so just make it String by writing .toString().

Andy Developer
  • 3,071
  • 1
  • 19
  • 39
  • Thanks for sharing but my app is now stopping. I don't know the reason as the logcat is not specifying the reason of the error. Could you please help me in this. My code is fine. But still i'm getting an unknown error. I did that as per your instructions. **got the error it is stating that Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference at in.pinelane.myhovi.AddFragment$ImageAdapter.getCount** – Alok Jul 20 '17 at 07:43
  • @AlokKumar it is because your arraylist is null. Please check the null condition before fetching the record. – Andy Developer Jul 20 '17 at 09:03
  • I did that it is working fine now after doing some changes! – Alok Jul 20 '17 at 10:08
1
  1. contains expect to see the same object type as the list items. if you want to check if the file is an image read this: Android: How to check if file is image?
  2. Decode file expects to get a file url and not a file object. see here how to use it: Bitmapfactory example
Sagi Mymon
  • 958
  • 9
  • 12