【问题标题】:Making function for checking is file name exist in external storage用于检查文件名是否存在于外部存储中的功能
【发布时间】:2016-06-20 06:39:09
【问题描述】:

我有两个活动。

1.TestActivity : 显示测试文档列表,当用户点击一个按钮将下载文档。

2.ShowDownloadedActivity:显示下载文件列表(外部存储文件列表)。

TestRecyclerAdapter 中,我需要一种方法来检查文件是否已经下载(换句话说,文件是否存在于外部存储中?)?如果它已经下载,则必须为用户显示不同的文本(例如“已下载”)。

为此,我想检查文件名是否存在于文件列表中(“外部存储”)。

这是 ShowDownloadedActivity 的onCreate

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_downloaded_file);

        String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/my.package.name/files/downloaded".toString();

        File file = new File(path);
        File list[] = file.listFiles();

        if(file.listFiles() != null){
            for(int i=0; i<list.length ; i++){

                ModelShowFile modelShowFile = new ModelShowFile();
                modelShowFile.setTitle(list[i].getName());

                mylist.add(modelShowFile);
            }

            recyclerView = (RecyclerView)findViewById(R.id.recycler_ac_show_downloaded_file);
            recyclerView.setLayoutManager(new LinearLayoutManager(ShowDownloadedFileActivity.this));

            adapter = new AdapterRecyclerShowFile(ShowDownloadedFileActivity.this,mylist);
            recyclerView.setAdapter(adapter);
        }
        else {
            Toast.makeText(ShowDownloadedFileActivity.this , "Nothing yet !",Toast.LENGTH_SHORT).show();
        }
    }

编辑:

伙计们,我需要 ShowDownloadedActivity 中的一种方法,其中文件名作为字符串参数进行检查,即具有传递文件名的文件是否已存在于外部存储中?就是这样。

谢谢:

【问题讨论】:

  • 您有问题吗?
  • 您面临的问题是什么?并且您确定要按原样使用此行:“String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/my.package.name/files/downloaded".toString();"查看 android 文档以获取更多信息
  • 您的问题不清楚。请多解释
  • @яσѕρєяK 我更新问题
  • 您是否尝试过实现或只是要求我们为您做??

标签: java android android-recyclerview


【解决方案1】:

试试这个可能对你有帮助

 public boolean checkDownload(String filenameWithExtension)
 {
    File extStore = Environment.getExternalStorageDirectory();
    File myFile = new File(extStore.getAbsolutePath() + filenameWithExtension);
    return myFile.exists();
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-26
    • 1970-01-01
    • 2015-09-26
    • 2013-09-19
    相关资源
    最近更新 更多