0

I am making an android app and I want to show all videos in the downloads directory, using a Recyclerview into the activity. So far, I have tried the code below. It does not show any errors but when I switch to the activity in which the files are supposed to appear, all I see is a blank screen. Downloads.java(The activity)

package com.example.singleactivity;

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

import android.os.Bundle;
import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.util.ArrayList;

@RequiresApi(api = 30)
public class Downloads extends AppCompatActivity {


File downloads_path = new File(Environment.DIRECTORY_DOWNLOADS);
public static ArrayList<File> allmedialist = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_downloads);
    load_directory_files(downloads_path);
    RecyclerView recyclerView = findViewById(R.id.recyclerView);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));

    RecyclerViewAdapter recyclerViewAdapter = new RecyclerViewAdapter(this);
    recyclerView.setAdapter(recyclerViewAdapter);

}

public static void load_directory_files(File download_path){
    File[] filelist = download_path.listFiles();
    if(filelist != null && filelist.length > 0){
        for(File file:filelist){
            if(file.isDirectory()){
                load_directory_files(file);
            }
            else{
                String name = file.getName().toLowerCase();
                if(name.endsWith(".mp4")){
                    allmedialist.add(file);
                    break;
                }
            }
        }
    }
}
}

RecyclerView Class

package com.example.singleactivity;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

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

import com.bumptech.glide.Glide;

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context mcontext;
RecyclerViewAdapter(Context mcontext){
    this.mcontext = mcontext;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.files_list,parent,false);
    return new FileLayoutHolder(view);
}

@RequiresApi(api = 30)
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

    ((FileLayoutHolder)holder).title.setText(Downloads.allmedialist.get(position).getName());

    Uri uri = Uri.fromFile(Downloads.allmedialist.get(position));

    Glide.with(mcontext).load(uri).thumbnail(0.1f).into(((FileLayoutHolder)holder).thumbnail);

}

@RequiresApi(api = 30)
@Override
public int getItemCount() {
    return Downloads.allmedialist.size();
}
static class FileLayoutHolder extends RecyclerView.ViewHolder{

    ImageView thumbnail;
    TextView title;
    ImageButton ic_more_btn;

    public FileLayoutHolder(@NonNull View itemView) {
        super(itemView);
        thumbnail = itemView.findViewById(R.id.thumbnail);
        title = itemView.findViewById(R.id.videotitle);
        ic_more_btn = itemView.findViewById(R.id.ic_more_btn);

    }
}
}

activity_downloads.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Downloads">
<androidx.recyclerview.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/recyclerView"/>

</androidx.constraintlayout.widget.ConstraintLayout>

file_list.xml

<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">

<ImageView
    android:id="@+id/thumbnail"
    android:layout_width="110dp"
    android:layout_height="65dp"/>

<TextView
    android:id="@+id/videotitle"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_weight="1" />

<ImageButton
    android:id="@+id/ic_more_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_more"
    android:layout_gravity="center"
    android:paddingTop="20dp"
    android:paddingBottom="20dp"
    android:paddingRight="2dp"
    android:paddingLeft="5dp"
    android:paddingEnd="2dp"
    android:paddingStart="5dp"/>

</LinearLayout>

This is what I see in the activity

How can I get the desired result?

Ryan M
  • 18,333
  • 31
  • 67
  • 74

0 Answers0