Football-Is-My-Life
Football-Is-My-Life

Reputation: 1437

check file exist in assets folder in android

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')

    }
}

Upvotes: 2

Views: 10700

Answers (4)

Prilaga
Prilaga

Reputation: 857

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
}

Upvotes: 0

miralong
miralong

Reputation: 842

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();
}

Upvotes: 1

Nickolai Astashonok
Nickolai Astashonok

Reputation: 2888

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()
}

Upvotes: 2

colymore
colymore

Reputation: 12316

You can use AssetManager Api, for example:

AssetManager am = getAssets();
List<String> mapList = Arrays.asList(am.list(""));

Upvotes: 6

Related Questions