A
A
Alexander2014-03-09 21:10:11
Java
Alexander, 2014-03-09 21:10:11

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.

The code
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)  { }

(App just closed without AlertDialog)
Log

03-09 20:33:17.739 1264-1264/core.player.app E/Trace﹕ error opening trace file: No such file or directory (2)
03-09 20:33:17.747 1264-1264/core.player.app W/dalvikvm﹕ JNI WARNING: input is not valid Modified UTF-8: illegal start byte 0xa7
03-09 20:33:17.747 1264-1264/core.player.app W/dalvikvm﹕ string: 'test�.txt'
03-09 20:33:17.747 1264-1264/core.player.app W/dalvikvm﹕ in Ljava/io/File;.listImpl:(Ljava/lang/String;)[Ljava/lang/String; (NewStringUTF)
03-09 20:33:17.795 1264-1264/core.player.app A/libc﹕ Fatal signal 11 (SIGSEGV) at 0xdeadd00d (code=1), thread 1264 (core.player.app)
03-09 20:33:18.007 281-299/system_process W/ActivityManager﹕ Force removing ActivityRecord{538c6a70 core.player.app/.MainActivity}: app died, no saved state
03-09 20:33:18.015 281-376/system_process W/WindowManager﹕ Rebuild removed 3 windows but added 2
03-09 20:33:18.015 281-376/system_process W/WindowManager﹕ This window was lost: Window{538c887c Starting core.player.app paused=false}
03-09 20:33:18.015 281-376/system_process W/WindowManager﹕ mSession=Session{537cae1c uid 1000} [email protected]
mAttrs=WM.LayoutParams{(0,0)(fillxfill) sim=#20 ty=3 fl=#1830118 pfl=0x9 wanim=0x103028f}
Requested w=800 h=1184 mLayoutSeq=96
mBaseLayer=21000 mSubLayer=0 mAnimLayer=21015+0=21015 mLastLayer=21015
mToken=AppWindowToken{5394fb2c token=Token{5390b38c ActivityRecord{538c6a70 core.player.app/.MainActivity}}}
mRootToken=AppWindowToken{5394fb2c token=Token{5390b38c ActivityRecord{538c6a70 core.player.app/.MainActivity}}}
mAppToken=AppWindowToken{5394fb2c token=Token{5390b38c ActivityRecord{538c6a70 core.player.app/.MainActivity}}}
mViewVisibility=0x0 mHaveFrame=true mObscured=false
mSeq=0 mSystemUiVisibility=0x0
mGivenContentInsets=[0,0][0,0] mGivenVisibleInsets=[0,0][0,0]
mConfiguration={1.0 0mcc0mnc en_US sw400dp w400dp h567dp nrml port finger qwerty/v/v dpad/v s.4}
mHasSurface=true mShownFrame=[17.285706,34.427734][817.2857,1218.4277]
mFrame=[0,0][800,1184] last=[0,0][800,1184]
mSystemDecorRect=[0,50][800,1184] last=[0,50][800,1184]
Frames: containing=[0,0][800,1184] parent=[0,0][800,1184] display=[0,0][800,1184]
content=[0,50][800,1184] visible=[0,50][800,1184]
Cur insets: content=[0,50][0,0] visible=[0,50][0,0]
Lst insets: content=[0,50][0,0] visible=[0,50][0,0]
mSurface=Surface(name=Starting core.player.app, identity=16)
mDrawState=HAS_DRAWN mLastHidden=true
Surface: shown=false layer=21015 alpha=0.964063 rect=(17.285706,34.427734) 800.0 x 1184.0
mShownAlpha=0.964063 mAlpha=1.0 mLastAlpha=0.964063
mGlobalScale=1.0 mDsDx=0.97071135 mDtDx=0.0 mDsDy=0.0 mDtDy=0.96964777
mExiting=false mRemoveOnExit=false mDestroying=true mRemoved=false
03-09 20:33:18.015 281-376/system_process W/WindowManager﹕ Current app token list:
03-09 20:33:18.015 281-376/system_process W/WindowManager﹕ Final window list:
03-09 20:33:18.023 533-533/com.android.launcher W/EGL_emulation﹕ eglSurfaceAttrib not implemented
03-09 20:33:18.027 281-623/system_process W/InputMethodManagerService﹕ Window already focused, ignoring focus gain of: [email protected] attribute=null

If these files are in a folder, then the program crashes immediately, without even having time to open,
this is how the Android file manager sees them.
11afc2d15541.png
How to protect the program from such errors?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mintormo, 2014-03-09
@Mintormo

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)

And then use
Environment.getExternalStorageDirectory()

F
FoxInSox, 2014-03-10
@FoxInSox

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");
    }
}

Result.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question