检查androids资产文件夹中的文件是否存在?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3592836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:53:36  来源:igfitidea点击:

Check for file existence in androids assets folder?

android

提问by Allan

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.

我的应用程序在 assets 文件夹的子目录中有 .txt 文件。它读取那些 .txt 文件并将它们放入文本视图中。它工作得很好,没有问题。

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.

就像我说的,没有我插入代码来检查文件是否存在,一切正常。我只是想知道人们是否每次从资产中读取流时都使用 .exists() 语句。

采纳答案by smith324

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.

普通用户无法删除它们,但使用 root 手机的用户不知道自己在做什么……情况就不同了。

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 reallywant to.

如果你问我,不需要额外的代码。此外,如果您尝试打开一个不存在的文件,您将在某个时候抛出异常,如果您真的想要捕获该异常并显示一个对话框。

回答by njzk2

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

您可能会担心文件已被删除并且 apk 已辞职

You can check using:

您可以使用以下方法检查:

Arrays.asList(getResources().getAssets().list("")).contains("myFile")

回答by natronite

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) {
    is.close();
  }
}

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

如果您的文件位于assets/folder/file.ext,那么pathInAssets将是 "folder/file.ext"

回答by shridutt kothari

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

理想情况下,在构建 apk 之后,没有人可以从中删除任何资产,但是如果有人反编译并重新编译,则可能有可能。

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

虽然对于其他场景,当资产在运行时不存在于 apk 中时,我们可以检查资产的存在。

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,

在我们的应用程序中,我们提供了使用 gradle、ant 和 eclipse 构建应用程序的规定,并且对于每种构建机制,我们的一些资产文件都捆绑在 apk 中,有些则没有,因此要确定当前构建的 apk 中是否存在任何资产文件在运行时,

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) {
        e.printStackTrace();
    } finally {
        try {
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }  
    return false;
}

回答by Alex Curran

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 cando it but I think you need to be rooted or use adb.

我想你应该没问题。从我的手机中拥有一个根,我看不出有任何方法可以在不删除应用程序的情况下删除资产,因为它似乎都包含在 .apk 文件中。你可以做到,但我认为你需要root或使用adb。

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

无论如何,为了安全起见,我个人会用 try/catch 块包围任何读/写。

回答by miralong

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

Convert to function or method can be easy ;)

转换为函数或方法很容易;)