i'am trying to display all files in devices with specific extensions, but problem is i got wrong size on all files, it only display 22 bytes to all files in listView. and i want to get default picture like zip image, ISO image and image of APK.. where i do wrong
public class NotificationsFragment extends Fragment {
ListView listView;
String[] items;
private NotificationsViewModel notificationsViewModel;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
notificationsViewModel =
ViewModelProviders.of(this).get(NotificationsViewModel.class);
View root = inflater.inflate(R.layout.fragment_notifications, container, false);
listView = root.findViewById(R.id.app_list);
runtimePermission();
//final TextView textView = root.findViewById(R.id.text_notifications);
notificationsViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
@Override
public void onChanged(@Nullable String s) {
//textView.setText(s);
}
});
return root;
}
public void runtimePermission() {
Dexter.withContext(getContext()).withPermissions(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUDIO)
.withListener(new MultiplePermissionsListener() {
@Override
public void onPermissionsChecked(MultiplePermissionsReport multiplePermissionsReport) {
showList();
}
@Override
public void onPermissionRationaleShouldBeShown(List<PermissionRequest> list, PermissionToken permissionToken) {
permissionToken.continuePermissionRequest();
}
}).check();
}
public ArrayList<File> findList(File file) {
ArrayList<File> arrayList = new ArrayList<>();
File[] files = file.listFiles();
for (File singleFile : files) {
if (singleFile.isDirectory() && !singleFile.isHidden()) {
arrayList.addAll(findList(singleFile));
} else {
if (singleFile.getName().endsWith(".apk") || singleFile.getName().endsWith(".iso") || singleFile.getName().endsWith(".exe") || singleFile.getName().endsWith(".rar") || singleFile.getName().endsWith(".zip")) {
arrayList.add(singleFile);
}
}
}
return arrayList;
}
private void showList() {
final ArrayList<File> myApps = findList(Environment.getExternalStorageDirectory());
items = new String[myApps.size()];
for (int i = 0; i<myApps.size(); i ){
items[i] = myApps.get(i).getName().toString();
}
customAdapter customAdapter = new customAdapter();
listView.setAdapter(customAdapter);
}
public String readableFileSize(long size) {
if (size <= 0)
return "0";
final String[] units = new String[] { "byte", "kb", "mb", "gb", "tb" };
int digitGroups = (int) (Math.log10(size) / Math.log10(1024));
return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, digitGroups)) " " units[digitGroups];
}
class customAdapter extends BaseAdapter {
@Override
public int getCount() {
return items.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View myView = getLayoutInflater().inflate(R.layout.download_item, null);
TextView songName = myView.findViewById(R.id.download_name);
ShapeableImageView appImage = myView.findViewById(R.id.download_img);
TextView sizeApp = myView.findViewById(R.id.app_details);
TextView install = myView.findViewById(R.id.install_btn);
songName.setSelected(true);
songName.setText(items[position]);
sizeApp.setText(readableFileSize(items.length));
// it gives 22 bytes to all list, which is not true
//How to get default image of file into listview??
return myView;
}
}
}
CodePudding user response:
items
is your list. So items.length
is the length of the list, not the length of a file. Try sizeApp.setText(readableFileSize(items[position].length))
.