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

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

问题描述

 进口的java.io.File;
    文件夹=新的文件(Environment.getExternalStorageDirectory()+/ TollCulator);
    布尔成功= TRUE;
    如果(!folder.exists()){
        //Toast.makeText(MainActivity.this,目录不存在,创建它,Toast.LENGTH_SHORT).show();
        成功= folder.mkdir();
    }
    如果(成功){
        //Toast.makeText(MainActivity.this,目录下创建,Toast.LENGTH_SHORT).show();
    } 其他 {
        //Toast.makeText(MainActivity.this,失败 - 错误,Toast.LENGTH_SHORT).show();
    }
 

以上应该在我的SD卡创建一个文件夹,如果它不存在,如果它不那么什么也不做。虽然作品敬酒根据条件,但它并没有创建一个目录,当它不存在。不知道如何解决它?

我的清单是这样的:

 < XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=com.test.testing
    安卓版code =1
    机器人:VERSIONNAME =1.0>

    <使用-权限的Andr​​oid:名称=android.permission.WRITE_EXTERNAL_STORAG​​E/>

    <用途-SDK
        安卓的minSdkVersion =6
        机器人:targetSdkVersion =17/>

    <应用
        机器人:allowBackup =真
        机器人:图标=@可绘制/ ic_launcher
        机器人:标签=@字符串/ APP_NAME
        机器人:主题=@风格/ AppTheme>
        <活动
            机器人:名称=com.test.testing.MainActivity
            机器人:标签=@字符串/ APP_NAME>
            <意向滤光器>
                <作用机器人:名称=android.intent.action.MAIN/>

                <类机器人:名称=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
        < /活性GT;
    < /用途>

< /舱单>
 

更新:我更新了我的表现,以及更新了我的code,但它仍然没有建立在我的SD卡上的文件夹中。请记住,我使用Eclipse和直接运行应用程序,以我的手机(GNEX VZW)而不是使用AVD。

解决方案

添加此权限在清单
<使用-权限的Andr​​oid:名称=android.permission.WRITE_EXTERNAL_STORAG​​E/>

 文件夹=新的文件(Environment.getExternalStorageDirectory()+
                             文件分割符+TollCulator);
布尔成功= TRUE;
如果(!folder.exists()){
    成功= folder.mkdir();
}
如果(成功){
    //做一些事情上的成功
} 其他 {
    //做别的事情上失败
}
 

  

当u运行应用程序走得DDMS->文件Explorer的> MNT   文件夹 - > SD卡文件夹 - >拨打创建文件夹

 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.mkdir();
}
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天全站免登陆