Reputation: 11
Hello I'm new in Android Studio and I really don't know how fix this problem, I really tried every thing but I can't solve this.
Problem:
I'm trying to retrieve videos from folders but it's not
happening, you can see down below in the exampe images:
example image 1 and example image 2.
Here are codes of videofileActivity, maybe there be a problem I couldn't see, so please check it.
public class videoFileActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private ArrayList<MediaFill> videoFileArraylist = new ArrayList<>();
private VideoFileAdapter videoFileAdapter;
private String folderName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_file);
folderName = getIntent().getStringExtra("folderName");
getSupportActionBar().setTitle(folderName);
recyclerView = findViewById(R.id.video_rv);
recyclerView.setLayoutManager(new LinearLayoutManager(this , RecyclerView.VERTICAL,false));
recyclerView.setAdapter(videoFileAdapter);
showVideoFile();
}
@SuppressLint("NotifyDataSetChanged")
private void showVideoFile() {
videoFileArraylist = getVideoFile(folderName);
if(folderName != null && videoFileArraylist.size()>0){
videoFileAdapter = new VideoFileAdapter(videoFileArraylist,this);
recyclerView.setAdapter(videoFileAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this,RecyclerView.VERTICAL,false));
}else {
Toast.makeText(this, "can't find any video", Toast.LENGTH_SHORT).show();
}
}
@SuppressLint("StringFormatInvalid")
private ArrayList<MediaFill> getVideoFile(String folder_Name){
ArrayList<MediaFill> videoFile = new ArrayList<>();
String[] projection = {
MediaStore.Video.Media._ID,MediaStore.Video.Media.DATA,MediaStore.Video.Media.TITLE,
MediaStore.Video.Media.DURATION,MediaStore.Video.Media.DISPLAY_NAME,MediaStore.Video.Media.DATE_ADDED,
MediaStore.Video.Media.SIZE,
};
String selection = MediaStore.Video.Media.DATA + " like?";
String[] selectionArg = new String[]{"%"+folder_Name+"%"};
Cursor cursor =getApplicationContext().getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,projection,selection,selectionArg,null);
if(cursor != null && cursor.moveToNext()){
do{
String id = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID));
String title = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.TITLE));
String size = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE));
String displayName = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME));
String duration = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION));
String path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA));
String dataAdded = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATE_ADDED));
MediaFill mediaFill= new MediaFill(id, id,title,size,dataAdded,duration,displayName,path);
videoFileArraylist.add(mediaFill);
}while (cursor.moveToNext());
}
return videoFile;
}
}
Below is the folder Adapter, please check this out too
public class VideoFileAdapter extends RecyclerView.Adapter<VideoFileAdapter.ViewHolder> {
private final ArrayList<MediaFill> videoList ;
private final Context context;
public VideoFileAdapter(ArrayList<MediaFill> videoList, Context context) {
this.videoList = videoList;
this.context = context;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
CardView v = (CardView) LayoutInflater.from(parent.getContext())
.inflate(R.layout.vidoe_item, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.videoName.setText(videoList.get(position).getDisplayName());
String size = videoList.get(position).getSize();
holder.videoSize.setText(Formatter.formatFileSize(context,Long.parseLong(size)));
double milliseconds = Double.parseDouble(videoList.get(position).getDuration());
holder.videoDuration.setText(timeConversion((long) milliseconds));
Glide.with(context).load(new File(videoList.get(position).getPath())).into(holder.Thumbnail);
holder.menu_more.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(context, "Menu more", Toast.LENGTH_SHORT).show();
}
});
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(context, "Playing video", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public int getItemCount() {
AtomicReference<ArrayList<MediaFill>> videoList = new AtomicReference<>(new ArrayList<>());
return videoList.get().size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
ImageView Thumbnail , menu_more;
TextView videoName, videoSize, videoDuration;
public ViewHolder(@NonNull View itemView) {
super(itemView);
Thumbnail = itemView.findViewById(R.id.thumbnail);
menu_more = itemView.findViewById(R.id.video_menu_more);
videoName = itemView.findViewById(R.id.video_name);
videoDuration = itemView.findViewById(R.id.video_duration);
videoSize = itemView.findViewById(R.id.video_size);
}
}
@SuppressLint("DefaultLocale")
public String timeConversion(long value){
String videoTime;
int duration = (int) value;
int hrs = (duration/3600000);
int mns = (duration/60000)%60000;
int sec = duration%60000/1000;
if (hrs > 0){
videoTime = String.format("%02d:%02d:%02d",hrs, mns,sec);
}else {
videoTime = String.format("%02d:%02d", mns,sec);
}
return videoTime;
}
}
Also it is a Mediafile
public class MediaFill {
private String id;
private String title;
private String displayName;
private String size;
private String duration;
private String path;
private String dateAdded;
public MediaFill(String id, String title, String displayName, String size, String duration, String path, String dateAdded, String s) {
this.id = id;
this.title = title;
this.displayName = displayName;
this.size = size;
this.duration = duration;
this.path = path;
this.dateAdded = dateAdded;
}
public MediaFill(String path) {
this.path = path;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
public String getSize() {
return size;
}
public void setSize(String size) {
this.size = size;
}
public String getDuration() {
return duration;
}
public void setDuration(String duration) {
this.duration = duration;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public String getDateAdded() {
return dateAdded;
}
public void setDateAdded(String dateAdded) {
this.dateAdded = dateAdded;
}
}
Upvotes: 1
Views: 396
Reputation: 3765
You problem goes here in the adapter:
@Override
public int getItemCount() {
AtomicReference<ArrayList<MediaFill>> videoList = new AtomicReference<>(new ArrayList<>());
return videoList.get().size();
}
Here you create n new list which has a size 0. So, the number of items in the recycler view is 0. But, it should be the size of the list right? So, try this:
@Override
public int getItemCount() {
return videoList.size();
}
Upvotes: 1