سؤال

In my application I use the code below to list movies in Movies folder in storage. But since I upgraded my Nexus 7 tablet to Android 4.4 this code is not working (the toast at the end of the code pops up meaning the code did not find any files in that folder, of I dont know even if it can access that folder). Any idea what should I change or what is changed in the new update?

    private void populateSpinners() {

        Boolean foundVideoFiles;

        // videos spinner
        File videoFolder =
                Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES)
                        .getAbsoluteFile();

        if (videoFolder.listFiles() != null) {
            foundVideoFiles = true; //found some files
            ArrayAdapter<File> movieAdapter = new ArrayAdapter<File>(this,
                    android.R.layout.simple_spinner_dropdown_item, videoFolder.listFiles());
            _spinner_videos.setAdapter(movieAdapter);

            //Restore perviously selected video
            int spinnerValue = _preferences.getInt("spinner_videos", -1);
            if (spinnerValue != -1) {
                _spinner_videos.setSelection(spinnerValue);
            }
        } else {
            foundVideoFiles = false;
        }

        //Error handling
        if (foundVideoFiles == false) {
            Toast.makeText(this, "ERROR: NO MOVIES WERE FOUND IN" + videoFolder, Toast.LENGTH_LONG).show();
        }
}

If I use the Gallery app on the tablet, I can see the movies there, so the movies have not been erased during update.

هل كانت مفيدة؟

المحلول

Note that as of Android 4.4 (per Android 4.4 Important Behavior Changes:

Your app can not read shared files on the external storage when running on Android 4.4, unless your app has the READ_EXTERNAL_STORAGE permission. That is, files within the directory returned by getExternalStoragePublicDirectory() are no longer accessible without the permission.

Make sure you add that permission to your Manifest:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top