【问题标题】:Folder creates as a file文件夹创建为文件
【发布时间】:2013-01-24 15:52:50
【问题描述】:

CONFUSED:那些对问题投反对票的人,你能添加一些 cmets,为什么????我想说脏话,就是不想被封号。

我确实有一个应用程序,我想在 sdCard 中有一个文件夹,以便用户可以添加一些东西,然后我可以在应用程序中使用它。我使用以下代码创建文件夹:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //External storage
        boolean mExternalStorageAvailable = false;
        boolean mExternalStorageWriteable = false;
        String state = Environment.getExternalStorageState();

        if (Environment.MEDIA_MOUNTED.equals(state)) {
            // We can read and write the media
            mExternalStorageAvailable = mExternalStorageWriteable = true;
            Log.v("Storage","ablo to read and write");
            //create Beatss folder
            File direct = new File(android.os.Environment.getExternalStorageDirectory().getPath() + "/Beatss");
            Log.v("Storage",android.os.Environment.getExternalStorageDirectory().getPath() + "/Beatss");
                boolean success = true;
                if (!direct.exists()) {
                    success = direct.mkdirs();

                }
                if (success) {
                    // Do something on success
                    Log.v("Storage","Folder has been created");

                } else {
                    // Do something else on failure 

                }

        } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
            // We can only read the media
            mExternalStorageAvailable = true;
            mExternalStorageWriteable = false;
            Log.v("Storage","ablo to read only");
        } else {

            mExternalStorageAvailable = mExternalStorageWriteable = false;
            Log.v("Storage","no access");               
        }

我可以在 DDMS 中看到该文件夹​​已创建,但是当我通过 WindowsExplorer 时,我有一个文件而不是文件夹。那么,如何创建文件夹呢?

UPD1:所以,即使我在这个文件夹中创建了一些东西,我仍然将它作为一个文件保存在 Windows 资源管理器中,但在 DDMS 中我可以看到我有一个文件夹,其中还有另一个文件夹。那么,这里有什么问题?

UPD2:所以,在 logcat 中拆分direct 行,看看是否有真实路径:

01-24 21:14:04.368: V/Storage(12116): /mnt/sdcard/Beatss/

所以有..

UPD3:我无法显示我的所有代码,到目前为止它是 264 行。所有这些文件夹的创建都发生在 mainActivity 的 onCreate 上。 manifestFile 如下所示:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="uk.test
android:versionCode="1"
android:versionName="1.0"
android:installLocation="auto" >

<uses-sdk
    android:minSdkVersion="7"
    android:targetSdkVersion="16" />
<supports-screens         
              android:smallScreens= "true"
              android:normalScreens="true"
              android:largeScreens="true"
              android:xlargeScreens="false"                  
/>


    <uses-permission android:name="android.permission.INTERNET"/> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<application
    android:icon="@drawable/icon"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"
    android:screenOrientation="portrait"
    >
    <activity
        android:name=".Main"
        android:label="@string/app_name"
        android:screenOrientation="portrait" 
        android:clearTaskOnLaunch="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name="com.google.ads.AdActivity"
              android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>        
    <activity android:name=".MainActivity"
              android:label="@string/app_name"
              android:screenOrientation="portrait">
    </activity>
    <activity android:name=".SupportScreen"
              android:label="@string/app_name"
              android:screenOrientation="portrait">
    </activity>
        <activity android:name="com.tapjoy.TJCOffersWebView" android:configChanges="keyboardHidden|orientation" />
        <activity android:name="com.tapjoy.TapjoyFullScreenAdWebView" android:configChanges="keyboardHidden|orientation" />
        <activity android:name="com.tapjoy.TapjoyDailyRewardAdWebView" android:configChanges="keyboardHidden|orientation" />
        <activity android:name="com.tapjoy.TapjoyVideoView" android:configChanges="keyboardHidden|orientation" />

</application>
</manifest>

更新代码上方:

Logcat 说:

01-24 21:19:21.068: V/Storage(14088): /mnt/sdcard/Beatss/
01-24 21:19:21.138: V/Storage(14088): Folder has been created

【问题讨论】:

  • 如果您在此“文件夹”内创建文件或之后检查direct.isDirectory(),会发生什么情况? ;)
  • 到目前为止从未尝试过...有趣
  • 只是一个警告——不要让那些发现他们的 sdcard 的用户在 sdcard 上有十几个目录——养成存储在/sdcard/Android/my.pkg.name/..... 中的习惯,以使最终用户更容易清理,如果需要的话......;)
  • 对对对,先让make创建那个文件夹,然后再考虑管理
  • 似乎与question类似的问题。

标签: android android-file


【解决方案1】:

试试看

String PATH = Environment.getExternalStorageDirectory()+"/Folder123/";
            File file = new File(PATH);
            file.mkdirs();

【讨论】:

  • 它创建了一个文件夹,但我无法通过 WindowsExplorer 看到它。我现在的情况一样,所以没有帮助
  • 我添加了工作代码作为答案,它使用了与上述评论相同的技术。此代码应该可以工作,因为它使用与我发布的工作代码相同的技术。请注意,具有可移动 SD 卡的设备上有两张 SD 卡。 Windows 资源管理器将显示两个目录,其名称类似于 Device 和 Card。 getExternalStorageDirectory() 返回设备而不是可移动 SD 卡目录。
  • 谢谢.. 你还有别的方法吗?
【解决方案2】:

这是我在生产中的应用程序的代码 sn-p....

if (spaceAvailable > spaceRequired) {
        databasePath = new File(settingsDatabasePath, GC.APP_DIRECTORY);
        databasePath.mkdirs();
        settingsDatabasePath = databasePath.getPath().toString();
    }

settingsDatabasePath 是在首次安装应用程序时确定的。它可以是外部存储也可以是内部存储,具体取决于设备是否具有外部存储。另外,在这个sn-p之前,已经确定GC.APP_DIRECTORY(GC类中的一个字符串常量)还没有被创建。此外,在这个 sn-p 之后,进行测试以确保该目录已创建,它是可写的,并且它是一个目录。数据库和文件在 app 目录中创建,以防止在根目录中“飞溅”文件。

注意:settingsDatabasePath 被故意覆盖。如果您复制代码,您可能不想进行覆盖。

【讨论】:

  • 你居然放了一半的东西,这段代码有什么用?起点在哪里?我已经厌倦了很多方法来创建该死的文件夹,但它创建的是一个文件而不是文件夹。
  • 关键代码与 gabrielaugustodm 的重复。使用 mkdirs() 创建整个目录字符串。 mkdir() 仅在现有目录树上创建最后一个目录。如果失败,两者都将返回 false。两者都不会创建文件。您的问题似乎是试图在目录应该是的级别上创建一个与现有文件同名的目录。或者,您正在不可移动(系统)sd 卡上创建结构。基本上,我是说 file.mkdirs() 过程是正确的。您遇到名称冲突或使用两张 SD 卡而不是一张的问题。
  • 问题是,您发布的所有代码也与我在问题中发布的代码相同。它确实创建了一个文件夹,在 android 系统内你可以毫无问题地使用它。我无法将其视为 WindowsExplorer 中的文件夹,这就是问题所在,而不是创建文件夹。如何使其作为文件夹工作
  • 好的,你正在创建一个目录。您如何尝试在目录中创建文件?
  • 我只想创建一个空的目录集,以便用户将其计算机中的数据填充到其中。
【解决方案3】:

试试这个:

String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
    File folder = new File(extStorageDirectory, "DIR_NAME");
    folder.mkdir();
    File file = new File(folder, "Sample.txt");
    try {
        file.createNewFile();
    } catch (IOException e1) {
        e1.printStackTrace();
    }

【讨论】:

    【解决方案4】:

    您的代码没有任何问题。我在我所有的应用程序上都使用相同的代码,它们工作得很好。 我复制/粘贴了您的代码,效果很好。在我的 Windows 机器上可以查看文件夹“Beatss”。您的 SD 卡一定有问题。您要么需要检查它是否有错误,要么尝试另一张 sd 卡。

    在 Windows 中右键单击 SD 卡,然后选择属性>工具>错误检查,然后单击“立即检查”。如果没有错误,请尝试另一张卡。如果不是这样,那么一起在另一台设备上尝试您的 apk,看看它是否在那里工作。

    【讨论】:

    • 我在两部手机上都试过了,不行。。不是闪存卡。我可能必须在其他制造商那里尝试,我所有的测试设备都来自索尼。
    猜你喜欢
    • 2016-10-03
    • 2019-08-07
    • 2018-09-03
    • 2018-12-02
    • 2011-04-24
    • 2019-03-31
    • 2013-09-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多