android - 如何在android studio中刷新 Assets 文件列表

标签 android list file assets

我有一个问题。

我想将文件从 assets forder(路径:Assets/Manual)复制到另一个目录(Environment.DIRECTORY_DOWNLOADS)。

所以我写了如下的源代码。

private void copyAssetManual(){
    AssetManager assetManager = getAssets();
    String[] files = null;
    try{
        files = assetManager.list("Manual");
        Log.d(TAG, "Files String Array Length: " + files.length);
    }catch(IOException e){
        Log.d(TAG, e.getMessage());
    }

    for(String filename : files){
        Log.d(TAG, "copyAssetManual: " + filename);
        InputStream in = null;
        OutputStream out = null;
        try{
            in = assetManager.open("Manual/" + filename);
            out = new FileOutputStream(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString() + "/" + filename);
            copyFile(in, out);
            in.close();
            in = null;
            out.flush();
            out.close();
            out = null;
        }catch(Exception e){
            Log.d(TAG, e.getMessage());
        }

        //Auto Execute Copied File
        File userManual = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), filename);
        String extension = MimeTypeMap.getFileExtensionFromUrl(filename);
        String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension.toLowerCase());
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(userManual), mimeType);
        startActivity(intent);
    }
}

private void copyFile(InputStream in, OutputStream out) throws IOException{
    byte[] buffer = new byte[1024];
    int read;
    while((read = in.read(buffer)) != -1){
        out.write(buffer, 0, read);
    }
}

它确实有效!

但是,当我替换 Assets 文件夹(路径: Assets /手册)中的文件(相同文件而不是不同文件名)时,此源代码记录了所有以前的文件名列表。

例)

Assets Folder has AAA.pdf

This source code printed "copyAssetManual: AAA.pdf"

After that, I deleted AAA.pdf and copy BBB.pdf to Assets Folder(Path: Assets/Manual)

then this source code printed twice like below.

"copyAssetManual: AAA.pdf"

"copyAssetManual: BBB.pdf"

Assets 文件夹中现在没有 AAA.pdf!

我该如何处理这个问题?

我不需要以前的文件名列表(AAA.pdf)....

请帮帮我..

最佳答案

您不能将文件复制到 Assets 文件夹中。 在您从 Assets 文件夹复制到外部 SD 卡文件系统的第一个代码 fragment 中

关于android - 如何在android studio中刷新 Assets 文件列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43674045/

相关文章:

java - 无法解析符号 'listener'

c# - 如何在 C# 中将 FSharpList 转换回 List?

c - C中的简单文件压缩

file - 在 gnuplot 中读取外部数据文件

python - 文件对象对比文件名

java - 编译android_x86 iso镜像时出错

具有多种 View 类型和 ItemTouchHelper 的 Android RecyclerView

java - 创建相机 View 后,Android 自动对焦不起作用

r - 将列表转换为 R 中的数据框并添加带有子列表名称的列

python - 元组构造函数与列表组合