Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9238475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:46:16+00:00 2026-06-18T07:46:16+00:00

I want to implement both memory and disk cache for images. Investigating I’ve found

  • 0

I want to implement both memory and disk cache for images. Investigating I’ve found this link and sample code (you can download it from the link on the right)

http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html

Somewhere in code there is this method:

/**
 * Get a usable cache directory (external if available, internal otherwise).
 *
 * @param context The context to use
 * @param uniqueName A unique directory name to append to the cache dir
 * @return The cache dir
 */
public static File getDiskCacheDir(Context context, String uniqueName) {
    // Check if media is mounted or storage is built-in, if so, try and use external cache dir
    // otherwise use internal cache dir
    final String cachePath =
            Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) ||
                    !isExternalStorageRemovable() ? getExternalCacheDir(context).getPath() :
                            context.getCacheDir().getPath();

    return new File(cachePath + File.separator + uniqueName);
}

I’m wondering what’s the rationale behind this check:

Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) ||
                    !isExternalStorageRemovable()

The second part seems redundant to me. This can be read as “even if the external storage is not mounted let’s use it because it can not be removed” but obviously you can not use it for caching since it is not mounted.

Funny thing is happening on the emulator with this code. It crashes on AVD based on Galaxy Nexus and SD card not specified. The first part will return false (it see it as “removed”) and the second will return true (because “external” storage is not removable on GN). Thus it will try to use external storage and it will crash since it’s can not use it.

I’ve tested with my Galaxy Nexus to see what is the first part value when phone is hooked up to pa PC or Mac and it is true both times. It is still mounted but the PC or Mac can write to it non the less.

In case you need them here are other used methods in the above code:

/**
 * Check if external storage is built-in or removable.
 *
 * @return True if external storage is removable (like an SD card), false
 *         otherwise.
 */
@TargetApi(9)
public static boolean isExternalStorageRemovable() {
    if (Utils.hasGingerbread()) {
        return Environment.isExternalStorageRemovable();
    }
    return true;
}

/**
 * Get the external app cache directory.
 *
 * @param context The context to use
 * @return The external cache dir
 */
@TargetApi(8)
public static File getExternalCacheDir(Context context) {
    if (Utils.hasFroyo()) {
        return context.getExternalCacheDir();
    }

    // Before Froyo we need to construct the external cache dir ourselves
    final String cacheDir = "/Android/data/" + context.getPackageName() + "/cache/";
    return new File(Environment.getExternalStorageDirectory().getPath() + cacheDir);
}

bonus question: Is someone using this code in production? Is this a good idea?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T07:46:18+00:00Added an answer on June 18, 2026 at 7:46 am

    Posting my own comment as an answer. It may be helpful to someone else. :

    getExternalStorageDirectory doesn’t always return the SDCard. That’s why the security check was implemented.

    Similar answer i had posted here and it is a good practice to always check for it.

    hope this will give you some hint about the double check.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I implement both of these ObservableCollections in my xaml? I want to
i starter in jqgrid, i want implement inline edit in jqgrid i have this
given this class definition: public class Frame { IFrameStream CapturedFrom; } I want implement
I want to do the same thing as in this link: http://www.codeproject.com/KB/WCF/Custom_Authorization_WCF.aspx But without
I have two UIbuttons,and I want to implement Longpressgesture on both. So I wrote
Im getting this error when trying to implement both onClickListener and onLongClickListener for a
I have been using this link to implement my two screen tabbed view, http://developer.android.com/training/implementing-navigation/lateral.html
I want to implement a behavior similar to Whatsapp, where when the user can
I've run into this multiple times, and can't find anything comprehensive. I want to
I read both iPhone and Google Map for iPhone EULA and want to implement

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.