Recently Added

6/recent/ticker-posts

Delete Folder and show folder properties Video player App Part 18

In this part, I am going to show you how to delete a folder in a proper way and show the properties of  that folder
so let's start



1.open the folder_view.xml file and add an ImageView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="6dp"
>

<ImageView
android:layout_weight="0"
android:layout_width="70dp"
android:layout_height="70dp"
android:src="@drawable/folder"/>

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

<TextView
android:id="@+id/folderName"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="folderName"
android:textColor="@color/white"
android:textSize="18sp"/>

<TextView
android:id="@+id/videosCount"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="10"
android:textColor="@color/light_white"/>

</LinearLayout>

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

</LinearLayout>

2.open FolderAdapter.java file and add this code
package com.example.player.adapter;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

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 com.google.android.material.bottomsheet.BottomSheetDialog;

import java.io.File;
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;
}

public static long getAllFileSize(File file) {

long size = 0; // Store total size of all files

if (file.isDirectory()) {
File[] files = file.listFiles(); // All files and subdirectories
for (int i = 0; files != null && i < files.length; i++) {
size += getSize(files[i]); // Recursive call
}
} else {
size += file.length();
}

return size;
}

public static long getSize(File file) {
long size = 0; // Store total size of all files

if (file.isDirectory()) {
File[] files = file.listFiles(); // All files and subdirectories
for (int i = 0; files != null && i < files.length; i++) {
size += getSize(files[i]); // Recursive call
}
} else {
size += file.length();
}

return size;
}

@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 onlyFolderName = folderName.get(position).substring(index + 1);

holder.name.setText(onlyFolderName);
String videoCount = String.valueOf(countVideos(folderName.get(position)));
holder.countVideos.setText(videoCount);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, VideoFolder.class);
intent.putExtra("folderName", folderName.get(position));
context.startActivity(intent);
}
});
holder.menu.setOnClickListener(v -> {
BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(context, R.style.BottomSheetDialogTheme);
View bottomSheetView = LayoutInflater.from(context).inflate(R.layout.file_menu, null);
bottomSheetDialog.setContentView(bottomSheetView);
bottomSheetDialog.show();
bottomSheetView.findViewById(R.id.menu_down).setOnClickListener(v1 -> {
bottomSheetDialog.dismiss();
});
bottomSheetView.findViewById(R.id.menu_share).setVisibility(View.GONE);
bottomSheetView.findViewById(R.id.menu_rename).setVisibility(View.GONE);
bottomSheetView.findViewById(R.id.menu_delete).setOnClickListener(v4 -> {
bottomSheetDialog.dismiss();
String path = folderName.get(position);
deleteFiles(path, position);
});
bottomSheetView.findViewById(R.id.menu_properties).setOnClickListener(v5 -> {
bottomSheetDialog.dismiss();
showProperties(onlyFolderName, videoCount, position);
});
});

}

private void deleteFiles(String path, int position) {
boolean isExists = isDirectoryExits(path);
if (isExists) {
boolean isDeleted = deleteDirectory(path);
if (isDeleted) {
Toast.makeText(context, "deleted",
Toast.LENGTH_SHORT).show();
folderName.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, folderName.size());
deleteFromMediaDatabase(path);
} else {
Toast.makeText(context, "fail to delete", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(context, "file already deleted",
Toast.LENGTH_SHORT).show();
folderName.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, folderName.size());
}
}

public boolean isDirectoryExits(String path) {
return new File(path).exists();
}

private boolean deleteDirectory(String path) {
return deleteDirectoryImp(path);
}

private boolean deleteDirectoryImp(String path) {
File directory = new File(path);
if (directory.exists()) {
File[] files = directory.listFiles();
if (files == null) {
return true;
}
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
deleteDirectoryImp(files[i].getAbsolutePath());
deleteFromMediaDatabase(files[i].getAbsolutePath());
} else {
files[i].delete();
deleteFromMediaDatabase(files[i].getAbsolutePath());
}
}
}
return directory.delete();
}

private void deleteFromMediaDatabase(String path){
try{
MediaScannerConnection.scanFile(context, new String[]{path},
null, new MediaScannerConnection.OnScanCompletedListener() {
@Override
public void onScanCompleted(String path, Uri uri) {
context.getContentResolver().delete(uri, null, null);
}
});
}catch (Exception e){
e.getStackTrace();
}
}

private void showProperties(String onlyFolderName, String videoCount, int position) {
Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.file_properties);

String path = folderName.get(position);

TextView title = dialog.findViewById(R.id.pro_title);
TextView location = dialog.findViewById(R.id.pro_storage);
TextView folderSize = dialog.findViewById(R.id.pro_size);
TextView videoCountName = dialog.findViewById(R.id.file_properties_videoCount);
TextView count = dialog.findViewById(R.id.pro_duration);
LinearLayout lastLayout = dialog.findViewById(R.id.file_properties_last_layout);
lastLayout.setVisibility(View.GONE);

title.setText(onlyFolderName);
location.setText(path);
videoCountName.setText("Video count: ");
long s = getAllFileSize(new File(path));
String converted = VideoFolder.fileReadableSize(s);
folderSize.setText(converted);
count.setText(videoCount);
dialog.show();
dialog.setCancelable(true);
}



@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;
ImageView menu;

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

Post a Comment

1 Comments