在android中转义或忽略文件和目录名称的特殊字符

时间:2015-06-24 07:36:22

标签: android special-characters

转义或忽略android

中文件和目录名的特殊字符

我正在尝试下面的代码

import java.io.File;
import java.util.ArrayList;

import javax.crypto.spec.PSource;

import android.R.string;
import android.support.v7.app.ActionBarActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterViewFlipper;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

    ListView lv;
    String[] items;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv =  (ListView) findViewById(R.id.lvPlaylist);
        final ArrayList<File> mySongs = findSongs(Environment.getExternalStorageDirectory());
        items =  new String[mySongs.size () ];
        for (int i = 0; i < mySongs.size(); i++) {
            //toast(mySongs.get(i).getName().toString());
            items[i] = mySongs.get(i).getName().toString();
        }

        ArrayAdapter<String> adp = new ArrayAdapter<String>(getApplicationContext(),R.layout.list_item,R.id.textView1,items);
        lv.setAdapter(adp);
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub
                startActivity(new Intent(getApplicationContext(), player.class).putExtra("pos", position).putExtra("songlist",mySongs));
            }
        });
    }

    public ArrayList<File> findSongs(File root){
        ArrayList al = new ArrayList<File>();
        File[] files = root.listFiles();
        for(File singleFile : files){
            if (singleFile.isDirectory()&& !singleFile.isHidden()) {
                al.addAll(findSongs(singleFile));

            }
            else {
                if (singleFile.getName().endsWith(".mp3") || singleFile.getName().endsWith(".wav")) {
                    al.add(singleFile);
                }
            }
        }
        return al;
    }

    public void toast(String text) {
        Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }



    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

问题出现在代码的这一部分

public ArrayList<File> findSongs(File root){
        ArrayList al = new ArrayList<File>();
        File[] files = root.listFiles();
        for(File singleFile : files){
            if (singleFile.isDirectory()&& !singleFile.isHidden()) {
                al.addAll(findSongs(singleFile));

            }
            else {
                if (singleFile.getName().endsWith(".mp3") || singleFile.getName().endsWith(".wav")) {
                    al.add(singleFile);
                }
            }
        }
        return al;
    }

如果是文件或目录WITH ANY SPECIAL CHARACTERS,则无法识别文件

如何忽略文件名和目录中的特殊字符

1 个答案:

答案 0 :(得分:0)

如果我理解正确您正在尝试阅读mp3文件,您可以尝试

    private class MP3Filter implements FilenameFilter
    {
            public boolean accept(File dir, String name)
            {
                return (name.endsWith(".mp3"));
            }

    }
   private void getMp3FilesName()
    {
        File home = new File(getDownloadsDir());
        if (tracksReaded == null)
            tracksReaded = new ArrayList<String>();
        MP3Filter filter = new MP3Filter();
        if (home.listFiles(filter) != null && home.listFiles(filter ).length > 0)
        {
            for (File file : home.listFiles(new MP3Filter()))
            {
                tracksReaded.add(file.getName());
            }
        }
    }