Recently Added

6/recent/ticker-posts

How To Load only Video Folders from storage and show in Recycler View Part 2

In this Part 2, I'm  going to load all only video folder and show in Recyclerview and also show how many videos inside the folders 

So let's start.



1.first we will create files_view.xml inside the layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:orientation="horizontal">


<androidx.cardview.widget.CardView
android:layout_width="150dp"
android:layout_height="90dp"
android:layout_margin="5dp"
android:layout_weight="0"
app:cardCornerRadius="5dp">

<ImageView
android:id="@+id/thumbnail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@mipmap/ic_launcher" />

</androidx.cardview.widget.CardView>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:orientation="vertical">

<TextView
android:id="@+id/video_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:gravity="start"
android:maxLines="3"
android:text="@string/long_text"
android:textAlignment="textStart"
android:textColor="@color/white"
android:textSize="15sp" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:id="@+id/video_duration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:text="00:00:00"
android:textColor="@color/light_white" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginHorizontal="3dp"
android:text="@string/text_wth_bullets"
android:textColor="@color/light_white" />

<TextView
android:id="@+id/video_quality"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:text="1080p"
android:textColor="@color/light_white" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginHorizontal="3dp"
android:text="@string/text_wth_bullets"
android:textColor="@color/light_white" />

<TextView
android:id="@+id/video_size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:text="1.15GB"
android:textColor="@color/light_white" />


</LinearLayout>
</LinearLayout>

<ImageView
android:id="@+id/video_menu"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_weight="0"
android:src="@drawable/menu_more" />

</LinearLayout>

2.Add RecyclerView inside activity_main.xml in layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".activity.MainActivity"
xmlns:app="http://schemas.android.com/apk/res-auto">

<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:title="@string/app_name"
app:titleTextColor="@color/white"/>

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/folder_recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</LinearLayout>

3.Create an Adapter and name it like FolderAdapter.java
package com.example.player.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.player.R;
import com.example.player.activity.VideoFolder;
import com.example.player.VideoModel;

import java.util.ArrayList;

public class FolderAdapter extends RecyclerView.Adapter<FolderAdapter.MyViewHolder> {

private final ArrayList<String> folderName;
private final ArrayList<VideoModel> videoModels;
private final Context context;

public FolderAdapter(ArrayList<String> folderName, ArrayList<VideoModel> videoModels, Context context) {
this.folderName = folderName;
this.videoModels = videoModels;
this.context = context;
}

@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.folder_view, parent, false);
return new MyViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {

int index = folderName.get(position).lastIndexOf("/");
String folderNames = folderName.get(position).substring(index + 1);

holder.name.setText(folderNames);
holder.countVideos.setText(String.valueOf(countVideos(folderName.get(position))));
}

@Override
public int getItemCount() {
return folderName.size();
}

int countVideos(String folders) {
int count = 0;
for (VideoModel model : videoModels) {
if (model.getPath().substring(0,
model.getPath().lastIndexOf("/"))
.endsWith(folders)) {
count++;
}
}
return count;
}

public class MyViewHolder extends RecyclerView.ViewHolder {
TextView name, countVideos;

public MyViewHolder(@NonNull View itemView) {
super(itemView);
name = itemView.findViewById(R.id.folderName);
countVideos = itemView.findViewById(R.id.videosCount);
}
}
}


4.Add this code inside your ManActivity.java
package com.example.player.activity;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.player.R;
import com.example.player.VideoModel;
import com.example.player.adapter.FolderAdapter;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {


FolderAdapter folderAdapter;
RecyclerView recyclerView;
private final ArrayList<String> folderList = new ArrayList<>();
private ArrayList<VideoModel> videoList = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

recyclerView = findViewById(R.id.folder_recyclerview);
videoList = fetchAllVideos(this);
if (folderList != null && folderList.size() > 0 && videoList != null) {
folderAdapter = new FolderAdapter(folderList, videoList, this);
recyclerView.setAdapter(folderAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this,
RecyclerView.VERTICAL, false));
} else {
Toast.makeText(this, "can't find any videos folder", Toast.LENGTH_SHORT).show();
}

}


//this is the method will fetch all videos from internal or external storage
private ArrayList<VideoModel> fetchAllVideos(Context context) {
ArrayList<VideoModel> videoModels = new ArrayList<>();
Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
String orderBy = MediaStore.Video.Media.DATE_ADDED + " DESC";
String[] projection = {
MediaStore.Video.Media._ID,
MediaStore.Video.Media.DATA,
MediaStore.Video.Media.TITLE,
MediaStore.Video.Media.SIZE,
MediaStore.Video.Media.HEIGHT,
MediaStore.Video.Media.DURATION,
MediaStore.Video.Media.DISPLAY_NAME,
MediaStore.Video.Media.RESOLUTION};

Cursor cursor = context.getContentResolver().query(uri, projection, null, null, orderBy);
if (cursor != null) {
while (cursor.moveToNext()) {
String id = cursor.getString(0);
String path = cursor.getString(1);
String title = cursor.getString(2);
String size = cursor.getString(3);
String resolution = cursor.getString(4);
String duration = cursor.getString(5);
String disName = cursor.getString(6);
String width_height = cursor.getString(7);

VideoModel videoFiles = new VideoModel(id, path, title, size, resolution, duration, disName, width_height);
int slashFirstIndex = path.lastIndexOf("/");
String subString = path.substring(0, slashFirstIndex);
if (!folderList.contains(subString)) {
folderList.add(subString);
}
videoModels.add(videoFiles);
}
cursor.close();
}
return videoModels;

}

}

Watch full Video Tutorial

Post a Comment

1 Comments

  1. When I add this code in Mainactivity.java
    "recyclerView = findViewByid(R.id.folder_recyclerview);"
    Then "folder_recyclerview" goes red warning
    What should I do?
    Create I'd value or something else?
    Please help

    ReplyDelete