Android 6.0 Marshmallow. Cannot write to SD Card

I faced the same problem. There are two types of permissions in Android: Dangerous (access to contacts, write to external storage…) Normal Normal permissions are automatically approved by Android while dangerous permissions need to be approved by Android users. Here is the strategy to get dangerous permissions in Android 6.0 Check if you have the … Read more

How can I read a text file from the SD card in Android?

In your layout you’ll need something to display the text. A TextView is the obvious choice. So you’ll have something like this: <TextView android:id=”@+id/text_view” android:layout_width=”fill_parent” android:layout_height=”fill_parent”/> And your code will look like this: //Find the directory for the SD Card using the API //*Don’t* hardcode “/sdcard” File sdcard = Environment.getExternalStorageDirectory(); //Get the text file File … Read more

Android get free size of internal/external memory

Below is the code for your purpose : public static boolean externalMemoryAvailable() { return android.os.Environment.getExternalStorageState().equals( android.os.Environment.MEDIA_MOUNTED); } public static String getAvailableInternalMemorySize() { File path = Environment.getDataDirectory(); StatFs stat = new StatFs(path.getPath()); long blockSize = stat.getBlockSizeLong(); long availableBlocks = stat.getAvailableBlocksLong(); return formatSize(availableBlocks * blockSize); } public static String getTotalInternalMemorySize() { File path = Environment.getDataDirectory(); StatFs stat … Read more

How can I get the external SD card path for Android 4.0+?

I have a variation on a solution I found here public static HashSet<String> getExternalMounts() { final HashSet<String> out = new HashSet<String>(); String reg = “(?i).*vold.*(vfat|ntfs|exfat|fat32|ext3|ext4).*rw.*”; String s = “”; try { final Process process = new ProcessBuilder().command(“mount”) .redirectErrorStream(true).start(); process.waitFor(); final InputStream is = process.getInputStream(); final byte[] buffer = new byte[1024]; while (is.read(buffer) != -1) { … Read more

How to use the new SD card access API presented for Android 5.0 (Lollipop)?

Lots of good questions, let’s dig in. 🙂 How do you use it? Here’s a great tutorial for interacting with the Storage Access Framework in KitKat: https://developer.android.com/guide/topics/providers/document-provider.html#client Interacting with the new APIs in Lollipop is very similar. To prompt the user to pick a directory tree, you can launch an intent like this: Intent intent … Read more