xralf
xralf

Reputation: 3692

Access removable SD card

I'm using this code to access SD card:

import os
from os.path import join
from jnius import autoclass
#from android.permissions import request_permissions, Permission

#request_permissions([Permission.WRITE_EXTERNAL_STORAGE,
#                     Permission.READ_EXTERNAL_STORAGE])
Environment = autoclass('android.os.Environment')
self.working_directory = os.path.join(Environment.getExternalStorageDirectory().getAbsolutePath(), "my_app_dir")
if not os.path.exists(self.working_directory):
  os.makedirs(self.working_directory)

Howerver I noticed that the data is created only on internal SD card. How can I access removable phone SD card and which permissions do I need (I need to read and write there)? My data is quite large, so I need it to save on removable SD card.

Upvotes: 2

Views: 501

Answers (1)

ximaera
ximaera

Reputation: 2468

First off, "external storage" is not the same as "removable storage". The external storage is basically defined by Google originally as "a storage which isn't always accessible", e.g. because it is mounted to a PC via USB. It's safe to say that since Android 4 this concept has lost most of the meaning.

Which kind of storage is actually used by the system as "external" is completely up to the device manufacturer. You should not assume that it's the removable media. Neither should you assume the removable media has more available storage space, or even exists on the system.

If there is one, though, then the correct way to handle it in your case is to use Context.getExternalFilesDirs(). This method would then return multiple paths over which you would need to iterate, checking File.getFreeSpace() to figure out which directory has got more available storage space.

(Hint: on a typical phone with a removable SD card the second item in the list returned by getExternalFilesDirs() is probably the removable media but that's a dirty hack.)

A modern approach would be to study the Storage Access Framework, here's the documentation.

Upvotes: 2

Related Questions