如何解决Android从文件资源管理器过滤文件
| 嗨,我正在使用一个简单的文件浏览器来编写我正在编写的应用程序的一部分,但是我想做的是,如果该文件在onclick中以.zip结尾,我想做除其他文件之外的其他事情到目前为止,这是我的课程,谢谢您的帮助public class Installed extends ListActivity {
private List<String> item = null;
private List<String> path = null;
private String root= \"/\";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.installed);
getDir(root);
}
private void getDir(String dirPath)
{
item = new ArrayList<String>();
path = new ArrayList<String>();
File f = new File(dirPath);
File[] files = f.listFiles();
if(!dirPath.equals(root))
{
item.add(root);
path.add(root);
item.add(\"../\");
path.add(f.getParent());
}
for(int i=0; i < files.length; i++)
{
File file = files[i];
path.add(file.getPath());
if(file.isDirectory())
item.add(file.getName() + \"/\");
else
item.add(file.getName());
}
ArrayAdapter<String> fileList =
new ArrayAdapter<String>(this,R.layout.row,item);
setListAdapter(fileList);
}
@Override
protected void onListItemClick(ListView l,View v,int position,long id) {
File file = new File(path.get(position));
if (file.isDirectory())
{
if(file.canRead())
getDir(path.get(position));
else
{
Toast.makeText(Installed.this,file.getName() + \" can\'t be read!\",Toast.LENGTH_LONG).show();
}
}
else
{
if(file.toString().endsWith(\".zip\")){
Toast.makeText(Installed.this,file.getName() + \" is zip\",Toast.LENGTH_LONG).show();
}else{
Toast.makeText(Installed.this,file.getName() + \" isn\'t zip\",Toast.LENGTH_LONG).show();
}
}
}
}
解决方法
只要您可以依靠文件名检查文件名是否以ZIP扩展名结尾,否则就可以检查魔术代码。对于zip文件,它应该是\“ PK \”(0x50 4B)
//--snip
else
{
if (file.getName().toUpperCase().endsWith(\".ZIP\") ){
//Do something with the zip file
}
}
//--snip
问候
, 尝试这个
file.toString().endsWith(\".zip\")
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。