Answer the question
In order to leave comments, you need to log in
Why does the application crash when reading a file with "Windows-1252" character in the name?
All folders except this read. It crashed until it deleted the file with the "Windows-1252" character in the name.
try
{
ArrayList<String> fileStr = new ArrayList<String>();
File files = new File("/mnt/sdcard/Download/Club/");
if(files.length() > 0)
{
for(File f : files.listFiles())
{
fileStr.add(f.getAbsolutePath().toString());
}
((TextView)findViewById(R.id.textView1)).setText("Файлов: " + fileStr.size());
ListView lv = (ListView)findViewById(R.id.listView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.item,fileStr);
lv.setAdapter(adapter);
}
}
catch (Exception ex) { }
Answer the question
In order to leave comments, you need to log in
In general, the SD card should not be treated that way. First you need to check if it is mounted.
Environment.getExternalStorageState()
.equals(Environment.MEDIA_MOUNTED)
Environment.getExternalStorageDirectory()
I can't reproduce your problem. Those. most likely either this is your local problem, or the error occurs in another part of the code. The presence of any characters in the file name should not be a problem. If desired, you can leave only readable characters for the user.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = (TextView) findViewById(R.id.textView);
File sdCard = Environment.getExternalStorageDirectory();
File[] list = sdCard.listFiles();
for (File f : list) {
String fileAbsPath = f.getAbsolutePath().toString();
fileAbsPath = fileAbsPath.replaceAll("[^\\x20-\\x7e]", "");
textView.append(fileAbsPath);
textView.append("\n");
}
textView.append("\n");
for (File f : list) {
String fileAbsPath = f.getAbsolutePath().toString();
textView.append(fileAbsPath);
textView.append("\n");
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question