Allan Jiang
Allan Jiang

Reputation: 11341

Android Phone Storage

I am working on an Android App. One thing I noticed is when I want to use storage, there are basically two options for me:

  1. Use package storage (code below):

    public static String getPackagePath(Activity activity){
            return activity.getFilesDir().toString();
    }
    
  2. if there is an SD card, I can use the external storage:

    /**
     * check if the phone has SD card
     * @return
     */
    public static boolean hasSDCard(){
            boolean fHasSDCard = false;
    
            if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){
                    fHasSDCard = true;
            }
    
            return fHasSDCard;
    }
    
    /**
     * get external storage directory path
     * @return
     */
    public static String getExternalStoragePath(){
    
            String strPath = "";
    
            if(hasSDCard()){
                    strPath = Environment.getExternalStorageDirectory().getPath();
            }
    
            return strPath;
    }
    

Now my code is mostly relay on SD card case, and most likely will break if there's no SD card. My question is, is there any android device with no SD card? If there is no SD card, is it the correct way to put data into the package path?

Thank you

Upvotes: 0

Views: 84

Answers (1)

Daverix
Daverix

Reputation: 399

There are devices without sdcards. They have a partition on it's internal memory instead, that you can call a shared memory. Looking at the docs on developer.android.com you can read this about getExternalStorageDirectory:

Note: don't be confused by the word "external" here. This directory can better be thought as media/shared storage. It is a filesystem that can hold a relatively large amount of data and that is shared across all applications (does not enforce permissions). Traditionally this is an SD card, but it may also be implemented as built-in storage in a device that is distinct from the protected internal storage and can be mounted as a filesystem on a computer.

So as Chris commented above, there should always be a sdcard partition if the device is not mounted to the computer via usb storage or similar. Your method for checking this should work fine. If it isn't available, tell the user to unmount the phone if connected.

But you may ask yourself: "Do I want other applications to access this data?". If not, your best bet are on using the internal storage. This may be a problem for some devices that do use a sdcard and doesn't have that much internal memory. Maybe check both storages to see if there is enough room?

Upvotes: 1

Related Questions