2

how to check file exist in assets folder in android ?

I'm using Android Studio and it doesn't seem like I have a assets folder. So I created one.

I'm using this code to load my fonts :

File pdfFile = null;
try {
    pdfFile = new File(new URI(("file:///android_assets/tahoma.ttf")));
    if (pdfFile.exists())
        Toast.makeText(MainActivity.this,"Exist",Toast.LENGTH_LONG).show();
    else
        Toast.makeText(MainActivity.this,"No Exist",Toast.LENGTH_LONG).show();
} catch (URISyntaxException e) {
    e.printStackTrace();
}

structure for a project in Android Studio 0.5.2:

root-module
|--.idea
|--app
|----build
|----src
|------main
|--------assets
|----------tahoma.ttf
|--------java
|----------source code here
|--------res
|------AndroidManifest.xml
|----build.gradle

buidl.gradle file :

apply plugin: 'android'

android {
    compileSdkVersion 19
    buildToolsVersion '19.1.0'
    sourceSets {
        main {
            assets.srcDirs = ['assets']
        }
    }
    defaultConfig {
        minSdkVersion 11
        targetSdkVersion 17
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile files('libs/android-support-v4.jar')

    }
}
Football-Is-My-Life
  • 1,407
  • 7
  • 18
  • 35

4 Answers4

6

You can use AssetManager Api, for example:

AssetManager am = getAssets();
List<String> mapList = Arrays.asList(am.list(""));
colymore
  • 11,776
  • 13
  • 48
  • 90
  • 2
    `Arrays.asList(getResources().getAssets().list("")).contains("myFile")` one liner from http://stackoverflow.com/a/7337516/1815624 – CrandellWS Jan 09 '17 at 10:03
2

You should use the following code to check is file exist or not:

AssetManager assetManager = getResources().getAssets();

try {
  InputStream inputStream =assetManager.open(PATH_TO_YOUR_FILE);

} catch (IOException ex) {
  ex.printStackTrace();
} finally {
   inputStream.close()
}
Nickolai Astashonok
  • 2,878
  • 3
  • 20
  • 23
1
AssetManager am = getAssets();

try {
    List<String> mapList = Arrays.asList(am.list("path/in/assets/folder"));

    if (mapList.contains("file_to_check")) {
        Log.e("ERROR", "exists");
    } else {
        Log.e("ERROR", "not exists");
    }
} catch ( IOException ex){
    ex.printStackTrace();
}
miralong
  • 789
  • 8
  • 11
0

We can iterate and get all files with a path. Empty directories are not included in the result.

/**
 * Example list of path to files:
 * "archive/wow.zip"
 * "audience_network.dex"
 * "font.ttf"
 * "images/clock_font.png"
 * "webkit/android-weberror.png"
 */
fun getAssetFilesList(context: Context, path: String = ""): List<String> {
    val routes = ArrayList<String>()
    context.assets.list(path)?.forEach { file ->
        val route = if (path.isNullOrEmpty()) file else "$path/$file"
        val names: List<String> = getAssetFilesList(context, route)
        if (names.isNotEmpty()) {
            routes.addAll(names)
        } else {
            routes.add(route)
        }
    }
    return routes
}
Prilaga
  • 818
  • 10
  • 18