• 3

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191


File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

Check for file existence in androids assets folder?

My app has .txt files in subdirectories in the assets folder. It reads those .txt files and puts them in a textview. It's working great and no problems.

Should I be concerned about the files in the assets folder getting deleted by the user or missing. If this ever could happen, my app would get an error because the file would not be there when it tried to read it into the stream.

Is there a need for me to check the existence of an asset file before I read it or does the asset manager take care of it all? I also was wondering if there's a chance that a user would or could delete and asset file.

Like I say, everything works fine without me inserting code to check for file existence. I just wondered if people use the .exists() statement every time they go to read in a stream from assets.

You may be concerned that the file have been removed and the apk resigned

You can check using:

  • 72
Reply Report

if you really want to check for the file existence:

AssetManager mg = getResources().getAssets();
InputStream is = null;
try {
  is = mg.open(pathInAssets);
  //File exists so do something with it
} catch (IOException ex) {
  //file does not exist
} finally {
  if (is != null) {

If your file is located in assets/folder/file.ext, then pathInAssets would be "folder/file.ext"

  • 27
Reply Report

Ideally after apk is built, nobody can remove any assets from it, but if someone decompiled it and recompiles than it may be possible.

Though for other scenarios also when an asset is not present in apk at Runtime, we can check the existence of asset.

In our app, we have a provision to build app using gradle, ant and eclipse, and for each build mechanism some of our assets file are bundled in apk and some are not, so to identify if any asset file is present in current build apk at runtime,

we do this as follows:

private boolean isAssetExists(String pathInAssetsDir){
    AssetManager assetManager = AppContext.get().getResources().getAssets();
    InputStream inputStream = null;
    try {
        inputStream = assetManager.open(pathInAssetsDir);
        if(null != inputStream ) {
           return true;
    }  catch(IOException e) {
    } finally {
        try {
        } catch (IOException e) {
    return false;
  • 7
Reply Report

Yes and No.

A normal user would not be able to delete them, but a user on a rooted phone who doesn't know what they're doing… that's a different situation.

If you ask me, the extra code is not needed. Also if you try and open a file that doesn't exist, you will get an exception thrown at some point, catch that and display a dialog if you really want to.

  • 5
Reply Report
      • 1
    • Great! Thanks Chris. I do have a 'try/catch' deal going on in the code but I wondered about a user being able to access the assets. Hey, try to say, "Access the assets" five times fast. Ha!
      • 2
    • More than that -- the only way to "delete" them is to rebuild the .apk without them, which requires also resigning it with a different cert. If you crash because someone did this, they more than deserve it. :p

I think you should be OK. From having a root around in my phone I can't see any way of deleting the assests without deleting the app as it all seems to be wrapped up in the .apk file. You can do it but I think you need to be rooted or use adb.

I would personally surround any reading/writing with a try/catch block anyway, just to be safe.

  • 1
Reply Report
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){

Convert to function or method can be easy ;)

  • 1
Reply Report