【问题标题】:How to get public storage path? External storage (not SD card)如何获取公共存储路径?外部存储(不是 SD 卡)
【发布时间】:2013-05-25 13:24:29
【问题描述】:

我想获取我的 android 设备的公共存储路径。我有两个应用程序。一个是写一些日志文件,另一个是用来读取它们(因此我不能使用应用程序私有存储)。我想知道是否有一种方法可以为我提供设备“公共空间”的路径,我可以在其中轻松创建和读取文件。

这个解决方案不是我想要的:

http://developer.android.com/guide/topics/data/data-storage.html#filesInternal

也不是:

How to get the internal and external sdcard path in android

我的问题有简单的解决方案吗? 我要的是外部公共存储吗?

http://developer.android.com/guide/topics/data/data-storage.html#filesExternal

问题是当我在我的设备上运行应用程序时一切正常,但是当我在没有存储卡的设备上运行它时,它就无法工作。所以我想使用外部公共存储,而不是存储卡...

我下面的代码不起作用(它不保存文件)。当我选择直接在 Environment.getExternalStorageDirectory().getAbsolutePath() 中的目录时,它可以工作......我做错了什么? :

transient private final String DIRECTORY = "/Android/data/com.aaa.bbb.ccc/files/";

public void writeLog()
{
    Calendar calendar = setDate();

    File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + DIRECTORY);
    if(!dir.exists())
        dir.mkdir();

    File file = new File (dir, "log_" + calendar.get(Calendar.YEAR) + "_"
                                 + ((calendar.get(Calendar.MONTH))+1) + "_"
                                 + calendar.get(Calendar.DAY_OF_MONTH)
                                 + ".dta");

...

}

更新:

为什么这段代码有效:

    File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Dir1/");
    if(!dir.exists())
        dir.mkdir();

这不是

    File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Dir1/Dir2/Dir3/");
    if(!dir.exists())
        dir.mkdir();

【问题讨论】:

    标签: java android file storage public


    【解决方案1】:

    试试看android.os.Environmenthere.

    以下方法似乎很有趣..

    public static File getDataDirectory ();
    //Gets the Android data directory. 
    
    public static File getDownloadCacheDirectory ();
    //Gets the Android download/cache content directory.  
    

    编辑: *解决方案:*

    那时你犯的一个小错误是

    替换这个:

    if(!dir.exists())
        dir.mkdir();
    

    与:

    if(!dir.exists())
        dir.mkdirs(); // will create all the parent directories as well..
    

    希望我现在帮助你 与

    【讨论】:

    • 为什么那个方法不好:getExternalStorageDirectory().getAbsolutePath 以及为什么我的代码不工作......它应该工作
    • 在您的代码中,问题是没有 SDCard 时。所以我建议你想办法将文件写入手机内存。如果 sdcard 不存在,你怎么能写入它?
    • 我使用的代码是在手机内存中写入可见的 SD 卡(我有 Galaxy S3)。问题是不同的。当我现在使用 DIRECTORY 时,它不起作用。但是当我只放入 DIRECTORY 一个目录时,比如 DIRECTORY = "/SomeDir/" 它工作得很好......为什么?
    • 我刚刚在您发布的同一时刻发现了这个错误。不过谢谢!让我检查一下阅读是否也有效
    • 是的,暂时没有什么能阻止你!
    【解决方案2】:

    在某些设备中,外部 SD 卡默认名称显示为 extSdCard,而对于其他设备,则显示为 sdcard1。这段代码 sn-p 有助于找出确切的路径,并有助于检索外部设备的路径。

    String sdpath, sd1path, usbdiskpath, sd0path; 
    if (new File("/storage/extSdCard/").exists()) {
        sdpath="/storage/extSdCard/";
        Log.i("Sd Cardext Path", sdpath);
    }
    if (new File("/storage/sdcard1/").exists()) {
        sd1path="/storage/sdcard1/";
        Log.i("Sd Card1 Path", sd1path);
    }
    if (new File("/storage/usbcard1/").exists()) {
        usbdiskpath="/storage/usbcard1/";
        Log.i("USB Path", usbdiskpath);
    }
    if (new File("/storage/sdcard0/").exists()) {
        sd0path="/storage/sdcard0/";
        Log.i("Sd Card0 Path", sd0path);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-25
      • 2021-12-31
      • 1970-01-01
      • 2012-12-08
      • 2022-01-21
      • 2013-04-15
      • 2016-11-23
      • 1970-01-01
      相关资源
      最近更新 更多