android - Can't check if file on sdcard exists -
i'm trying make simple check if file exist. saw similar questions here, didn't help. when run application, app crashes , got message "unfortunatelly, filecheck1 has stopped". got error both on emulator , smartphone.
my code:
package com.example.filecheck1; import android.app.activity; import android.os.bundle; import android.os.environment; import android.widget.textview; import java.io.file; public class myactivity extends activity { textview msgtext; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); msgtext = (textview) findviewbyid(r.id.textview); string path = environment.getexternalstoragedirectory().getpath()+"/ping.xml"; file file = getbasecontext().getfilestreampath(path); if(file.exists()){ msgtext.settext("found"); } if(!file.exists()){ msgtext.settext("not found"); } } }
in manifest such permissions:
<uses-permission android:name="android.permission.internet"></uses-permission> <uses-permission android:name="android.permission.write_external_storage"></uses-permission>
thanks in advance.
i think problem here:
getbasecontext()
where assigned null
. don't need line. can achieve goal with
string path = environment.getexternalstoragedirectory().getpath() + "/ping.xml"; file f = new file(path); if (f.exists()) { // stuff } else { // stuff }
update:
if or else have samsung galaxy s3, please follow @raghunandan's answer because in case getexternalstoragedirectory()
returns internal memory.
Comments
Post a Comment