【问题标题】:How to refresh assets file list in android studio如何在android studio中刷新资产文件列表
【发布时间】:2017-09-26 05:00:37
【问题描述】:

我有问题。

我想将文件从 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);
    }
}

确实有效!

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

例如)

资产文件夹有 AAA.pdf

此源代码打印“copyAssetManual: AAA.pdf”

之后,我删除了 AAA.pdf 并将 BBB.pdf 复制到 Assets 文件夹(路径:Assets/Manual)

然后这个源代码打印两次,如下所示。

"copyAssetManual: AAA.pdf"

"copyAssetManual: BBB.pdf"

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

我该如何处理这个问题?

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

请帮帮我..

【问题讨论】:

    标签: android list file assets


    【解决方案1】:

    我终于治疗了!

    只需单击代码清理菜单按钮。

    【讨论】:

      【解决方案2】:

      您不能将文件复制到资产文件夹中。 在第一个代码 sn -p 中,您从 assets 文件夹复制到外部 sd 卡文件系统中

      【讨论】:

        猜你喜欢
        • 2013-10-14
        • 2014-12-29
        • 1970-01-01
        • 2016-07-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多