mardi 21 juillet 2015

Android: How to check if a file exist on my sdcard

I`m trying to check if a file exist on my sdcard with this code but i have some problems. The api on my Android phone is 19 and the api of the application is 19 but there are a lot of exceptions from other application which i do not want to work with like zedge for example. Please give me some hints on how to check if that file exists.

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    File extStore = Environment.getExternalStorageDirectory();
    File myFile = new File(extStore.getAbsolutePath() + "/test.txt");

    if(myFile.exists()){
        Log.d("File", "exists");
    }

}


public boolean isExternalStorage() {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        return true;
    }
    return false;
}

}

My Manifest file is like this:

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://ift.tt/nIICcg"
    package="encryption.nikola.com.test" >

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Aucun commentaire:

Enregistrer un commentaire