在Android中创建文件夹 [英] Create folder in Android

查看:126
本文介绍了在Android中创建文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  import java.io.File; 
文件夹=新文件(Environment.getExternalStorageDirectory()+/ TollCulator);
boolean success = true;
if(!folder.exists()){
//Toast.makeText(MainActivity.this,目录不存在,创建它,Toast.LENGTH_SHORT).show();
success = folder.mkdir();
}
if(success){
//Toast.makeText(MainActivity.this,Directory Created,Toast.LENGTH_SHORT).show();
} else {
//Toast.makeText(MainActivity.this,Failed - Error,Toast.LENGTH_SHORT).show();
}

上述应该在SD卡中创建一个不存在的文件夹,如果那样做不做任何事情。虽然这个toast是基于条件的,但是当它不存在时它不会创建一个目录。任何想法如何解决?



我的清单如下所示:

 <?xml version =1.0encoding =utf-8?> 
< manifest xmlns:android =http://schemas.android.com/apk/res/android
package =com.test.testing
android:versionCode = 1
android:versionName =1.0>

< uses-permission android:name =android.permission.WRITE_EXTERNAL_STORAGE/>

< uses-sdk
android:minSdkVersion =6
android:targetSdkVersion =17/>

< application
android:allowBackup =true
android:icon =@ drawable / ic_launcher
android:label =@ string / app_name
android:theme =@ style / AppTheme>
< activity
android:name =com.test.testing.MainActivity
android:label =@ string / app_name>
< intent-filter>
< action android:name =android.intent.action.MAIN/>

< category android:name =android.intent.category.LAUNCHER/>
< / intent-filter>
< / activity>
< / application>

< / manifest>

更新:我更新了我的清单,并更新了我的代码,仍然没有创建我的SD卡中的文件夹。请记住,我正在使用Eclipse并将应用程序直接运行到我的手机(GNex VZW),而不是使用AVD。

解决方案

Manifest 中添加此权限,

< uses-permission android:name =android.permission.WRITE_EXTERNAL_STORAGE/ >

 文件夹=新建文件(Environment.getExternalStorageDirectory()+ 
文件.separator +TollCulator);
boolean success = true;
if(!folder.exists()){
success = folder.mkdirs();
}
如果(成功){
//成功执行某项操作
} else {
//执行其他失败
} $ b $当您运行应用程序时,您也可以使用DDMS->文件管理器 - > mnt
文件夹 - > sdcard文件夹 - >收费创建文件夹



 import java.io.File;  
    File folder = new File(Environment.getExternalStorageDirectory() + "/TollCulator");
    boolean success = true;
    if (!folder.exists()) {
        //Toast.makeText(MainActivity.this, "Directory Does Not Exist, Create It", Toast.LENGTH_SHORT).show();
        success = folder.mkdir();
    }
    if (success) {
        //Toast.makeText(MainActivity.this, "Directory Created", Toast.LENGTH_SHORT).show();
    } else {
        //Toast.makeText(MainActivity.this, "Failed - Error", Toast.LENGTH_SHORT).show();
    }

The above should create a folder in my SD card if it does not exist, if it does then don't do anything. Although the toast works based on the condition but it doesn't create a directory when it does not exist. Any idea how to resolve it?

My Manifest looks like this:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.test.testing"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <uses-sdk
        android:minSdkVersion="6"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.test.testing.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Update: I updated my manifest as well as updated my code but it's still not creating the folder in my SD card. Keep in mind, I am using Eclipse and running the app directly to my phone (GNex VZW) instead of using an AVD.

解决方案

Add this permission in Manifest,
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

File folder = new File(Environment.getExternalStorageDirectory() + 
                             File.separator + "TollCulator");
boolean success = true;
if (!folder.exists()) {
    success = folder.mkdirs();
}
if (success) {
    // Do something on success
} else {
    // Do something else on failure 
}

when u run the application go too DDMS->File Explorer->mnt folder->sdcard folder->toll-creation folder

这篇关于在Android中创建文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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