Can't check if file on sdcard exists

前端 未结 3 620
萌比男神i
萌比男神i 2020-12-04 00:54

I\'m trying to make a simple check if the file exist. I saw similar questions here, but they didn\'t help. When I run my application, the app crashes and I got message \"Unf

3条回答
  •  抹茶落季
    2020-12-04 01:30

    I think that problem is here:

    getBaseContext()
    

    where it is assigned to NULL. You really don't need this line. You can simply achieve your goal with

    String path = Environment.getExternalStorageDirectory().getPath() + "/ping.xml";
    File f = new File(path);
    if (f.exists()) {
       // do your stuff
    }
    else {
      // do your stuff
    }
    

    Update:

    If you or someone else have Samsung Galaxy S3, please follow @Raghunandan's answer because in this case getExternalStorageDirectory() returns internal memory.

提交回复
热议问题