Manager - File Manager

//Exanple Use

//insert a ListView and padding to 0.
// Custom Listview list_item.xml. On this page add an ImageView (imageview1) and a TextView (textview1) inside a Linear(H).
// Add another CustomView empty_view.xml. On this page add a TextView with text "Empty!".
// Add an Intent component i.
// Add a String variable myFile.
// Add two images, with name "folder" and "note"

//onCreate
Directory = new java.io.File("/"); //or /sdcard/
Files = new ArrayList <java.io.File >();

View emptyView = getLayoutInflater().inflate(R.layout.empty_view, null);
((ViewGroup)listview1.getParent()).addView(emptyView);
listview1.setEmptyView(emptyView);

Adapter = new FilePickerListAdapter(MainActivity.this, Files);
listview1.setAdapter(Adapter);
refreshFilesList();
}
protected java.io.File Directory;
protected ArrayList<java.io.File> Files;
protected FilePickerListAdapter Adapter;
protected boolean ShowHiddenFiles = false;
private class FilePickerListAdapter extends ArrayAdapter<java.io.File> {
private List<java.io.File> mObjects;

public FilePickerListAdapter(Context context, List<java.io.File> objects) {
super(context, R.layout.list_item, android.R.id.text1, objects);
mObjects = objects;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = null;
if(convertView == null) {
row = getLayoutInflater().inflate(R.layout.list_item, parent, false);
} else {
row = convertView;
}
java.io.File object = mObjects.get(position);
ImageView imageView = (ImageView)row.findViewById(R.id.imageview1);
TextView textView = (TextView)row.findViewById(R.id.textview1);
textView.setSingleLine(true);
textView.setText(object.getName());
if(object.isFile()){
imageView.setImageResource(R.drawable.note);
} else{
imageView.setImageResource(R.drawable.folder);
}
return row;
}
}
protected void refreshFilesList() {
Files.clear();
java.io.File[] files = Directory.listFiles();
if(files != null && files.length > 0) {
for(java.io.File f : files) {
if(f.isHidden() && !ShowHiddenFiles) {
continue;
}
Files.add(f);
}
Collections.sort(Files, new FileComparator());
}
Adapter.notifyDataSetChanged();
}

private class FileComparator implements Comparator <java.io.File> {
public int compare(java.io.File f1, java.io.File f2) {
if(f1 == f2) return 0;
if(!f1.isFile() && f2.isFile()) return -1;
if(f1.isFile() && f2.isDirectory()) return 1;
return f1.getName().compareToIgnoreCase(f2.getName());
}



//onResume
refreshFilesList();

//onBackPressed
if(Directory.getParentFile() != null) {
Directory = Directory.getParentFile();
refreshFilesList();
return;
}
super.onBackPressed();


//ListView onItemClicked.

java.io.File newFile = Files.get(_position);
if(newFile.isFile()) {
myFile= newFile.toString();
try {
//Source Block Intent setAction and select ACTION_VIEW.
i.setDataAndType(Uri.fromFile(newFile), MimeTypeMap.getSingleton().getMimeTypeFromExtension(myFile.substring(myFile.lastIndexOf(".") + 1)));
//Source Block StartActivity Intent
} catch (Exception e) {
showMessage(e.toString());
}
} else {
Directory = newFile;
refreshFilesList();
}


//Need Permission
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Komentar

Cara pembuatan Apps paling Populer

Create Stopwatch App in Android using Sketchware

TextInputLayout in Sketchware

How to find and​ highlight a word in a text field in Sketchware?

A Flash Light App in Sketchware

How to enable download in webview in Sketchware apps?

Intent - Open File By Type

Code for implementing Notifications in Sketchware

How to share an image from Drawable folder?

ActionBar back button

Animation Transtition Animation