Checking for html file of android folder

I am passing the html file as "file: ///android_asset/WebApplication/index.html". How can I check the existence of this file in android.

+3


source to share


2 answers


You can just try to open the stream, if it fails, the file isn't there, and if it doesn't fail, the file should be there:



/**
 * Check if an asset exists. This will fail if the asset has a size < 1 byte.
 * @param context
 * @param path
 * @return TRUE if the asset exists and FALSE otherwise
 */
public static boolean assetExists(Context context, String path) {
    boolean bAssetOk = false;
    try {
        InputStream stream = context.getAssets().open(ASSET_BASE_PATH + path);
        stream.close();
        bAssetOk = true;
    } catch (FileNotFoundException e) {
        Log.w("IOUtilities", "assetExists failed: "+e.toString());
    } catch (IOException e) {
        Log.w("IOUtilities", "assetExists failed: "+e.toString());
    }
    return bAssetOk;
}

      

+5


source


[Android Assets]: File or Folder?

How to get all files from a file folder



File f=new File("file:///android_asset/");  
    File[] files=f.listFiles();

      

and if the files return null you will know the folder is empty.

0


source







All Articles