Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
正在检查android资产文件夹html文件是否存在_Android_Android Assets - Fatal编程技术网

正在检查android资产文件夹html文件是否存在

正在检查android资产文件夹html文件是否存在,android,android-assets,Android,Android Assets,我将html文件传递为“file:///android_asset/WebApplication/index.html". 如何在android中检查此文件是否存在。您可以尝试打开流,如果失败,则文件不在那里,如果不失败,则文件应在那里: /** * Check if an asset exists. This will fail if the asset has a size < 1 byte. * @param context * @param path * @return T

我将html文件传递为“file:///android_asset/WebApplication/index.html". 如何在android中检查此文件是否存在。

您可以尝试打开流,如果失败,则文件不在那里,如果不失败,则文件应在那里:

/**
 * 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;
}
/**
*检查资产是否存在。如果资源的大小小于1字节,则此操作将失败。
*@param上下文
*@param路径
*@如果资产存在,则返回TRUE;否则返回FALSE
*/
公共静态布尔assetExists(上下文、字符串路径){
布尔bAssetOk=false;
试一试{
InputStream=context.getAssets().open(资产\基础\路径+路径);
stream.close();
巴塞托克=真;
}catch(filenotfounde异常){
w(“IOUtilities”,“assetExists失败:”+e.toString());
}捕获(IOE异常){
w(“IOUtilities”,“assetExists失败:”+e.toString());
}
返回巴塞托克;
}


如果文件返回null den,您就知道文件夹是空的。

您尝试过file.isExist吗?我正在使用“Arrays.asList(getResources().getAssets().list(“”)。contains(filename)”这段代码来检查assets文件夹中的文件。如何检查资产子目录中的文件。无需硬编码,如在列表(“”)方法中指定文件夹名称。一旦使用getResources().getAssets()这一数据,就很容易了。您知道资产文件夹中有多少文件夹,可以使用文件夹长度的循环,如果不是空的,可以检查文件夹中有多少文件(使用递归函数或如果文件夹有子文件夹,则反复调用)是的,我正在获取文件夹列表,但很难将其转换为文件或目录。“列表列表=数组。asList(getResources().getAssets().list(“”)”。我使用它列出目录。如何将其转换为文件。?u只需检查isDirectory函数,即可计算有多少目录。file filefolder=新文件(list.get(I).toString()),并检查条件filefolder.isDirectory()如果路径以
file:///android_asset/
然后重写so:final String ASSET_PATH=”file:///android_asset/“String filePath=path.replaceFirst(ASSET_path,”);InputStream=context.getAssets().open(filePath);注意:注释中的部分不适用于目录。
File f=new File("file:///android_asset/");  
    File[] files=f.listFiles();