Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Android

Android डिवाइस पर उपलब्ध स्टोरेज स्पेस कैसे चेक करें?

Android डिवाइस पर उपलब्ध मेमोरी/स्टोरेज चेक करने का तरीका

यह उदाहरण StatFs और Formatter का उपयोग करके इंटरनल और एक्सटर्नल स्टोरेज पर उपलब्ध स्थान को मानव-पठनीय फॉर्मेट में दिखाता है।

चरण 1: नया प्रोजेक्ट बनाएं

Android Studio में File → New Project चुनें।

चरण 2: लेआउट फ़ाइल (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    android:padding="8dp"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAlignment="center"
        android:textSize="24sp"
        android:textStyle="bold" />

</LinearLayout>

चरण 3: MainActivity.java

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Environment;
import android.os.StatFs;
import android.text.format.Formatter;
import android.widget.TextView;
import java.io.File;

public class MainActivity extends AppCompatActivity {
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = findViewById(R.id.textView);

        // 1. एक्सटर्नल स्टोरेज (शेयर्ड/एसडी कार्ड)
        File externalPath = Environment.getExternalStorageDirectory();
        StatFs statExternal = new StatFs(externalPath.getPath());
        long externalFreeBytes = statExternal.getAvailableBlocksLong() * statExternal.getBlockSizeLong();

        // 2. इंटरनल स्टोरेज (ऐप डेटा डायरेक्टरी)
        File internalPath = Environment.getDataDirectory();
        StatFs statInternal = new StatFs(internalPath.getPath());
        long internalFreeBytes = statInternal.getAvailableBlocksLong() * statInternal.getBlockSizeLong();

        // मानव-पठनीय फॉर्मेट में बदलें
        String externalFree = Formatter.formatFileSize(this, externalFreeBytes);
        String internalFree = Formatter.formatFileSize(this, internalFreeBytes);

        String result = "एक्सटर्नल फ्री: " + externalFree + "\n" +
                       "इंटरनल फ्री: " + internalFree;
        textView.setText(result);
    }
}

चरण 4: AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="app.com.sample">

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

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

आधुनिक API (Android 10+/API 29+)

// StorageManager उपयोग करें - स्कोप्ड स्टोरेज के लिए सही
StorageManager storageManager = getSystemService(StorageManager.class);
StorageVolume volume = storageManager.getPrimaryStorageVolume();
long freeBytes = volume.getFreeBytes();
long totalBytes = volume.getTotalBytes();
String free = Formatter.formatFileSize(this, freeBytes);

नोट

  • Environment.getExternalStorageDirectory() API 29+ में डिप्रिकेटेड है।
  • ऐप-विशिष्ट फाइल्स के लिए getFilesDir() या getExternalFilesDir() उपयोग करें।
  • Formatter.formatFileSize() बाइट्स को KB/MB/GB में ऑटो-कन्वर्ट करता है।
  1. Android में ConcurrentLinkedDeque में एलिमेंट मौजूद है या नहीं कैसे चेक करें? Android में ConcurrentLinkedDeque में एलिमेंट मौजूद है या नहीं कैसे चेक करें?

    ConcurrentLinkedDeque में contains() मेथड का उपयोग ConcurrentLinkedDeque एक थ्रेड-सेफ, अनबाउंडेड डीक (डबल-एंडेड क्यू) है जो लिंक्ड नोड्स पर आधारित है। मल्टी-थ्रेडेड वातावरण में सुरक्षित एक्सेस के लिए डिज़ाइन किया गया है। चरण 1: प्रोजेक्ट बनाएं Android Studio में नया प्रोजेक्ट: File → New Project।

  1. एंड्रॉइड डिवाइस में कॉल्स कैसे डिटेक्ट करें? एंड्रॉइड डिवाइस में कॉल्स कैसे डिटेक्ट करें?

    एंड्रॉइड में फोन स्टेट मॉनिटरिंग (कॉल डिटेक्शन) यह गाइड BroadcastReceiver और TelephonyManager का उपयोग करके इनकमिंग/आउटगोइंग कॉल्स डिटेक्ट करने का तरीका दिखाता है। आवश्यक अनुमतियाँ <uses-permission android:name=android.permission.READ_PHONE_STATE /> <!-- एंड्रॉइड 10+ के लिए फोरग्राउंड सर्व

  1. एंड्रॉइड में EditText खाली है या नहीं कैसे जांचें? एंड्रॉइड में EditText खाली है या नहीं कैसे जांचें?

    एंड्रॉइड में EditText खाली/वैलिड है या नहीं जांचना — बेस्ट प्रैक्टिसेज यूज़र इनपुट वैलिडेट करना हर ऐप की बुनियादी जरूरत है। TextUtils.isEmpty() सबसे सरल तरीका है, लेकिन **सिर्फ खाली स्ट्रिंग ही नहीं, व्हाइटस्पेस-ओनली स्ट्रिंग भी अमान्य माननी चाहिए**। यह ट्यूटोरियल सभी सामान्य पैटर्न कवर करता है। चर