Meine App funktioniert für Handys, die nur eine SD-Karte haben. Ich möchte also programmgesteuert prüfen, ob die SD-Karte verfügbar ist oder nicht und wie der freie Speicherplatz der SD-Karte zu finden ist. Ist es möglich?
Wenn ja, wie mache ich das?
Boolean isSDPresent = Android.os.Environment.getExternalStorageState().equals(Android.os.Environment.MEDIA_MOUNTED);
Boolean isSDSupportedDevice = Environment.isExternalStorageRemovable();
if(isSDSupportedDevice && isSDPresent)
{
// yes SD-card is present
}
else
{
// Sorry
}
Die akzeptierte Antwort funktioniert für mich nicht
Environment.getExternalStorageState().equals(Android.os.Environment.MEDIA_MOUNTED);
Falls das Gerät über einen eingebauten Speicher verfügt, wird true zurückgegeben Meine Lösung dient zur Überprüfung der Anzahl externer Verzeichnisse. Wenn es mehr als eine gibt, hat das Gerät eine SD-Karte. Es funktioniert und ich habe es für mehrere Geräte getestet.
public static boolean hasRealRemovableSdCard(Context context) {
return ContextCompat.getExternalFilesDirs(context, null).length >= 2;
}
Verwenden Sie Environment.getExternalStorageState()
wie in "Verwendung des externen Speichers" beschrieben.
Verwenden Sie StatFs
, um verfügbaren Speicherplatz auf einem externen Speicher zu erhalten.
// do this only *after* you have checked external storage state:
File extdir = Environment.getExternalStorageDirectory();
File stats = new StatFs(extdir.getAbsolutePath());
int availableBytes = stats.getAvailableBlocks() * stats.getBlockSize();
Ich habe eine kleine Klasse für die Überprüfung des Speicherstatus geschrieben. Vielleicht ist es für Sie von Nutzen.
import Android.os.Environment;
/**
* Checks the state of the external storage of the device.
*
* @author kaolick
*/
public class StorageHelper
{
// Storage states
private boolean externalStorageAvailable, externalStorageWriteable;
/**
* Checks the external storage's state and saves it in member attributes.
*/
private void checkStorage()
{
// Get the external storage's state
String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED))
{
// Storage is available and writeable
externalStorageAvailable = externalStorageWriteable = true;
}
else if (state.equals(Environment.MEDIA_MOUNTED_READ_ONLY))
{
// Storage is only readable
externalStorageAvailable = true;
externalStorageWriteable = false;
}
else
{
// Storage is neither readable nor writeable
externalStorageAvailable = externalStorageWriteable = false;
}
}
/**
* Checks the state of the external storage.
*
* @return True if the external storage is available, false otherwise.
*/
public boolean isExternalStorageAvailable()
{
checkStorage();
return externalStorageAvailable;
}
/**
* Checks the state of the external storage.
*
* @return True if the external storage is writeable, false otherwise.
*/
public boolean isExternalStorageWriteable()
{
checkStorage();
return externalStorageWriteable;
}
/**
* Checks the state of the external storage.
*
* @return True if the external storage is available and writeable, false
* otherwise.
*/
public boolean isExternalStorageAvailableAndWriteable()
{
checkStorage();
if (!externalStorageAvailable)
{
return false;
}
else if (!externalStorageWriteable)
{
return false;
}
else
{
return true;
}
}
}
Sie können so prüfen, ob eine externe, entfernbare SD-Karte verfügbar ist
public static boolean externalMemoryAvailable(Activity context) {
File[] storages = ContextCompat.getExternalFilesDirs(context, null);
if (storages.length > 1 && storages[0] != null && storages[1] != null)
return true;
else
return false;
}
Das funktioniert perfekt, wie ich es getestet habe.
Ich habe es so geändert, dass wenn eine SD-Karte vorhanden ist, der Pfad dort festgelegt wird. Wenn nicht, wird es im internen Verzeichnis abgelegt.
Boolean isSDPresent = Android.os.Environment.getExternalStorageState().equals(Android.os.Environment.MEDIA_MOUNTED);
if(isSDPresent)
{
path = theAct.getExternalCacheDir().getAbsolutePath() + "/GrammarFolder";
}
else
{
path = theAct.getFilesDir() + "/GrammarFolder";
}
void updateExternalStorageState() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
mExternalStorageAvailable = mExternalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
} else {
mExternalStorageAvailable = mExternalStorageWriteable = false;
}
handleExternalStorageState(mExternalStorageAvailable,
mExternalStorageWriteable);
}
Diese einfache Methode ist für mich gearbeitet. In allen Gerätetypen getestet.
public boolean externalMemoryAvailable() {
if (Environment.isExternalStorageRemovable()) {
//device support sd card. We need to check sd card availability.
String state = Environment.getExternalStorageState();
return state.equals(Environment.MEDIA_MOUNTED) || state.equals(
Environment.MEDIA_MOUNTED_READ_ONLY);
} else {
//device not support sd card.
return false;
}
}
Ich habe eine Klasse erstellt, um zu prüfen, ob der Ordner auf der SD-Karte verfügbar ist oder nicht:
public class GetFolderPath {
static String folderPath;
public static String getFolderPath(Context context) {
if (isSdPresent() == true) {
try {
File sdPath = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/FolderName");
if(!sdPath.exists()) {
sdPath.mkdirs();
folderPath = sdPath.getAbsolutePath();
} else if (sdPath.exists()) {
folderPath = sdPath.getAbsolutePath();
}
}
catch (Exception e) {
}
folderPath = Environment.getExternalStorageDirectory().getPath()+"/FolderName/";
}
else {
try {
File cacheDir=new File(context.getCacheDir(),"FolderName/");
if(!cacheDir.exists()) {
cacheDir.mkdirs();
folderPath = cacheDir.getAbsolutePath();
} else if (cacheDir.exists()) {
folderPath = cacheDir.getAbsolutePath();
}
}
catch (Exception e){
}
}
return folderPath;
}
public static boolean isSdPresent() {
return Android.os.Environment.getExternalStorageState().equals(Android.os.Environment.MEDIA_MOUNTED);
}
}
Kotlin
fun Context.externalMemoryAvailable(): Boolean {
val storages = ContextCompat.getExternalFilesDirs(this, null)
return storages.size > 1 && storages[0] != null && storages[1] != null
}
** i fixed this with help of @Jemo Mgebrishvili answer**
dies funktioniert auch dann einwandfrei, wenn eine SD-Karte vorhanden und im ausgeworfenen Zustand ist
if (ContextCompat.getExternalFilesDirs(this, null).length >= 2) {
File[] f = ContextCompat.getExternalFilesDirs(this, null);
for (int i = 0; i < f.length; i++) {
File file = f[i];
if(file!=null && i ==1)
{
Log.d(TAG,file.getAbsolutePath()+ "external sd card available");
}
}
} else {
Log.d(TAG, " external sd card not available");
}