Skip to content

Instantly share code, notes, and snippets.

@piyush-malaviya
Last active April 19, 2018 09:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save piyush-malaviya/003d3a24285cadf5da82ab3972a57bc5 to your computer and use it in GitHub Desktop.
Save piyush-malaviya/003d3a24285cadf5da82ab3972a57bc5 to your computer and use it in GitHub Desktop.
Get directories and files list using native methods
public class HomeActivity extends BaseAppCompatActivity {
// Used to load the 'native-lib' library on application startup.
static {
System.loadLibrary("native-lib");
}
public native Object[] getMediaFiles(String root);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home)
initViews()
}
private void initViews() {
final long start = System.currentTimeMillis();
Log.e(TAG, "Start: " + new Date(start).toString());
Object[] objects = getMediaFiles(System.getenv("ANDROID_STORAGE"));
for (Object object : objects) {
Log.e(TAG, "Path: " + object);
}
Log.e(TAG, "End: " + new Date().toString());
Log.e(TAG, "End Sec: " + TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - start));
}
}
#include <sys/types.h>
#include <stdlib.h>
#include <jni.h>
#include <string.h>
#include <dirent.h>
#include <stdio.h>
#include <errno.h>
#include <android/log.h>
#include <vector>
#include <string>
#define LOG_TAG "testjni"
#define ALOG(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
std::vector<std::string> vector;
bool isVideo(const char *file) {
return strstr(file, ".3g2") != NULL
|| strstr(file, ".3gp") != NULL
|| strstr(file, ".3gp2") != NULL
|| strstr(file, ".3gpp") != NULL
|| strstr(file, ".amv") != NULL
|| strstr(file, ".asf") != NULL
|| strstr(file, ".avi") != NULL
|| strstr(file, ".divx") != NULL
|| strstr(file, ".drc") != NULL
|| strstr(file, ".dv") != NULL
|| strstr(file, ".f4v") != NULL
|| strstr(file, ".flv") != NULL
|| strstr(file, ".gvi") != NULL
|| strstr(file, ".gxf") != NULL
|| strstr(file, ".ismv") != NULL
|| strstr(file, ".iso") != NULL
|| strstr(file, ".m1v") != NULL
|| strstr(file, ".m2v") != NULL
|| strstr(file, ".m2t") != NULL
|| strstr(file, ".m2ts") != NULL
|| strstr(file, ".m3u8") != NULL
|| strstr(file, ".mkv") != NULL
|| strstr(file, ".mov") != NULL
|| strstr(file, ".mp2") != NULL
|| strstr(file, ".mp2v") != NULL
|| strstr(file, ".mp4") != NULL
|| strstr(file, ".mp4v") != NULL
|| strstr(file, ".m4v") != NULL
|| strstr(file, ".mpe") != NULL
|| strstr(file, ".mpeg") != NULL
|| strstr(file, ".mpeg1") != NULL
|| strstr(file, ".mpeg2") != NULL
|| strstr(file, ".mpeg4") != NULL
|| strstr(file, ".mpg") != NULL
|| strstr(file, ".mpv2") != NULL
|| strstr(file, ".mts") != NULL
|| strstr(file, ".mtv") != NULL
|| strstr(file, ".mxf") != NULL
|| strstr(file, ".mxg") != NULL
|| strstr(file, ".nsv") != NULL
|| strstr(file, ".nut") != NULL
|| strstr(file, ".nuv") != NULL
|| strstr(file, ".ogm") != NULL
|| strstr(file, ".ogv") != NULL
|| strstr(file, ".ogx") != NULL
|| strstr(file, ".ps") != NULL
|| strstr(file, ".rec") != NULL
|| strstr(file, ".rm") != NULL
|| strstr(file, ".rmvb") != NULL
|| strstr(file, ".tod") != NULL
|| strstr(file, ".trp") != NULL
|| strstr(file, ".ts") != NULL
|| strstr(file, ".tts") != NULL
|| strstr(file, ".vob") != NULL
|| strstr(file, ".vro") != NULL
|| strstr(file, ".webm") != NULL
|| strstr(file, ".wm") != NULL
|| strstr(file, ".wmv") != NULL
|| strstr(file, ".wtv") != NULL
|| strstr(file, ".xesc") != NULL
|| strstr(file, ".3G2") != NULL
|| strstr(file, ".3GP") != NULL
|| strstr(file, ".3GP2") != NULL
|| strstr(file, ".3GPP") != NULL
|| strstr(file, ".AMV") != NULL
|| strstr(file, ".ASF") != NULL
|| strstr(file, ".AVI") != NULL
|| strstr(file, ".DIVX") != NULL
|| strstr(file, ".DRC") != NULL
|| strstr(file, ".DV") != NULL
|| strstr(file, ".F4V") != NULL
|| strstr(file, ".FLV") != NULL
|| strstr(file, ".GVI") != NULL
|| strstr(file, ".GXF") != NULL
|| strstr(file, ".ISMV") != NULL
|| strstr(file, ".ISO") != NULL
|| strstr(file, ".M1V") != NULL
|| strstr(file, ".M2V") != NULL
|| strstr(file, ".M2T") != NULL
|| strstr(file, ".M2TS") != NULL
|| strstr(file, ".M3U8") != NULL
|| strstr(file, ".MKV") != NULL
|| strstr(file, ".MOV") != NULL
|| strstr(file, ".MP2") != NULL
|| strstr(file, ".MP2V") != NULL
|| strstr(file, ".MP4") != NULL
|| strstr(file, ".MP4V") != NULL
|| strstr(file, ".M4V") != NULL
|| strstr(file, ".MPE") != NULL
|| strstr(file, ".MPEG") != NULL
|| strstr(file, ".MPEG1") != NULL
|| strstr(file, ".MPEG2") != NULL
|| strstr(file, ".MPEG4") != NULL
|| strstr(file, ".MPG") != NULL
|| strstr(file, ".MPV2") != NULL
|| strstr(file, ".MTS") != NULL
|| strstr(file, ".MTV") != NULL
|| strstr(file, ".MXF") != NULL
|| strstr(file, ".MXG") != NULL
|| strstr(file, ".NSV") != NULL
|| strstr(file, ".NUT") != NULL
|| strstr(file, ".NUV") != NULL
|| strstr(file, ".OGM") != NULL
|| strstr(file, ".OGV") != NULL
|| strstr(file, ".OGX") != NULL
|| strstr(file, ".PS") != NULL
|| strstr(file, ".REC") != NULL
|| strstr(file, ".RM") != NULL
|| strstr(file, ".RMVB") != NULL
|| strstr(file, ".TOD") != NULL
|| strstr(file, ".TRP") != NULL
|| strstr(file, ".TS") != NULL
|| strstr(file, ".TTS") != NULL
|| strstr(file, ".VOB") != NULL
|| strstr(file, ".VRO") != NULL
|| strstr(file, ".WEBM") != NULL
|| strstr(file, ".WM") != NULL
|| strstr(file, ".WMV") != NULL
|| strstr(file, ".WTV") != NULL
|| strstr(file, ".XESC") != NULL;
}
void list_dir(std::string dir_name) {
DIR *d;
/* Open the directory specified by "dir_name". */
d = opendir(dir_name.c_str());
/* Check it was opened. */
if (!d) {
ALOG("Cannot open directory '%s': %s\n", dir_name.c_str(), strerror(errno));
return;
}
while (1) {
struct dirent *entry;
std::string d_name;
/* "Readdir" gets subsequent entries from "d". */
entry = readdir(d);
if (!entry) {
/* There are no more entries in this directory, so break
out of the while loop. */
break;
}
d_name = entry->d_name;
if (entry->d_type & DT_DIR) {
/* Check that the directory is not "d" or d's parent. */
if (d_name.compare("..") != 0 && (d_name.compare(".")) != 0) {
/* Recursively call "list_dir" with the new path. */
list_dir(dir_name + "/" + d_name);
}
} else {
if (isVideo(d_name.c_str())) {
vector.push_back(dir_name + "/" + d_name);
}
}
}
/* After going through all the entries, close the directory. */
if (closedir(d)) {
ALOG("Could not close '%s': %s\n", dir_name.c_str(), strerror(errno));
return;
}
}
extern "C"
JNIEXPORT jobjectArray JNICALL
Java_com_technologo_getmediafilelist_activity_HomeActivity_getMediaFiles(JNIEnv *env, jobject obj,
jstring rootPath) {
const char *path = env->GetStringUTFChars(rootPath, NULL);
list_dir((char *) path);
/*for (unsigned long i = 0; i < vector.size(); i++) {
ALOG("Val %s\n", vector.at(i).c_str());
}*/
jclass jClass = env->FindClass("java/lang/Object");
jobjectArray array = env->NewObjectArray((jsize) vector.size(), jClass, 0);
for (unsigned long i = 0; i < vector.size(); i++) {
env->SetObjectArrayElement(array, (jsize) i, env->NewStringUTF(vector[i].c_str()));
}
return array;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment