保存文件在Android设备的内部存储 [英] Save file in the internal storage of android device

查看:140
本文介绍了保存文件在Android设备的内部存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使该保存在SD卡上的临时文件的应用程序。

I'm trying to make an application that save a temporary file on the sd card.

如果用户没有SD卡我想要的应用程序保存内部存储文件

if the user don't have a sd card I want the application save the file in internal storage

索里我的英语水平。

推荐答案

这是我使用的无论是SD卡或内部存储上的缓存,但要小心。你必须定期清除缓存,尤其是在内部存储。

This is what I use for caching on either the SD card, or the internal storage, but be careful. You have to regularly clear your cache, especially on the internal storage.

private static boolean sIsDiskCacheAvailable = false;
private static File sRootDir = null;

public static void initializeCacheDir(Context context){
    Context appContext = context.getApplicationContext();

    File rootDir = null;

    if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){
        // SD card is mounted, use it for the cache
        rootDir = appContext.getExternalCacheDir();
    } else {
        // SD card is unavailable, fall back to internal cache
        rootDir = appContext.getCacheDir();

        if(rootDir == null){
            sIsDiskCacheAvailable = false;
            return;
        }
    }

    sRootDir = rootDir;

    // If the app doesn't yet have a cache dir, create it
    if(sRootDir.mkdirs()){
        // Create the '.nomedia' file, to prevent the mediastore from scanning your temp files
        File nomedia = new File(sRootDir.getAbsolutePath(), ".nomedia");
        try{
            nomedia.createNewFile();
        } catch(IOException e){
            Log.e(ImageCache.class.getSimpleName(), "Failed creating .nomedia file!", e);
        }
    }

    sIsDiskCacheAvailable = sRootDir.exists();

    if(!sIsDiskCacheAvailable){
        Log.w(ImageCache.class.getSimpleName(), "Failed creating disk cache directory " + sRootDir.getAbsolutePath());
    } else {
        Log.d(ImageCache.class.getSimpleName(), "Caching enabled in: " + sRootDir.getAbsolutePath());

        // The cache dir is created, you can use it to store files
    }
}

这篇关于保存文件在Android设备的内部存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆