Android - Wait until external storage is ready

I am currently developing live wallpapers that are read from external storage. When the device boots up, I assume that you can run live wallpapers before the storage is ready. Especially if it performs periodic error checking. Others report problems, and I think that is the reason. I cannot verify this because external storage seems to be mounted on my device instantly, and I'm not sure how to get it to perform error checking. So my first question is whether the system is really intended to be BOOT_COMPLETED before it launches live wallpapers.

If not, what is the way to wait for external storage. I am going to name something like this at the beginning of the application.

public void waitForExternalStorage() { while(Environment.getExternalStorageState().equals(Environment.MEDIA_CHECKING)) { try { Thread.sleep(1000L); } catch(InterruptedException e) { e.printStackTrace(); } } } 

Do I need to check other cases if it is MEDIA_REMOVED → MEDIA_UNMOUNTED → MEDIA_CHECKING (optional) → MEDIA_READY at boot time?

+4
source share
3 answers

You can register BroadcastReceiver to listen for changes in the external storage state:

 BroadcastReceiver externalStorageStateReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { updateExternalStorageState(); } }; IntentFilter filter = new IntentFilter(); filter.addAction(Intent.ACTION_MEDIA_MOUNTED); filter.addAction(Intent.ACTION_MEDIA_REMOVED); registerReceiver(mExternalStorageReceiver, filter); updateExternalStorageState(); // You can initialize the state here, before any change happens 

And in updateExternalStorageState() you can check the actual state after the change:

 protected void updateExternalStorageState() { String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state)) { // SD card is mounted and ready for use } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { // SD card is mounted, but it is read only } else { // SD card is unavailable } } 
+6
source

use broadcast receiver, listen Intent.ACTION_MEDIA_MOUNTED : broadcast action: external media is present and mounted at the mount point.

+2
source

I need just that. I am reading a configuration file from an SD card, and my application, which loads at boot, simply cannot work without reading it. A simple solution is to wait a maximum of 15-30 seconds to automatically install the SD card on the Android OS. If not, throw an exception. Here is the code. Just increase the maximum number of limits to increase the waiting time. ExternalStorageNotReadyException is a custom exception.

 public void awaitExternalStorageInitialization() throws ExternalStorageNotReadyException { boolean mExternalStorageAvailable = false; boolean mExternalStorageWriteable = false; int count = 0; do { String state = Environment.getExternalStorageState(); if(count > 0) { try { Thread.sleep(3000); } catch (InterruptedException e) { Log.e(Constants.LOG_TAG, e.getMessage(), e); } } if (Environment.MEDIA_MOUNTED.equals(state)) { // We can read and write the media mExternalStorageAvailable = mExternalStorageWriteable = true; } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { // We can only read the media mExternalStorageAvailable = true; mExternalStorageWriteable = false; } else { // Something else is wrong. It may be one of many other states, // but all we need // to know is we can neither read nor write mExternalStorageAvailable = mExternalStorageWriteable = false; } count++; } while ((!mExternalStorageAvailable) && (!mExternalStorageWriteable) && (count < 5)); if(!mExternalStorageWriteable) throw new ExternalStorageNotReadyException("External storage device (SD Card) not yet ready"); } 
0
source

Source: https://habr.com/ru/post/1438020/


All Articles