package cn.com.android.sdcard;import java.io.File;import java.io.IOException;import android.app.Activity;import android.os.Bundle;import android.os.Environment;import android.util.Log;import android.widget.TextView;/** * @author chenzheng_java * @since 2011/03/02 * @description 对android基于sdcard的应用进行测试 * */public class SdCardActivity extends Activity {    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        String result = "";        //获取文件默认存储位置的绝对路径        File fileDir = this.getFilesDir();        Log.i("文件路径",fileDir.getPath());        result+="文件路径 /n fileDir.getPath():"+fileDir.getPath();        result+="/n fileDir.getAbsolutePath():"+fileDir.getAbsolutePath();        result+="/n fileDir.getName():"+fileDir.getName();                // 获取外部存储设备的当前状态        String externalStorageState = Environment.getExternalStorageState();       // Environment.MEDIA_MOUNTED代表着外部存储设备存在,并且是可读写的        if(externalStorageState.equals(Environment.MEDIA_MOUNTED)){        // getExternalStorageDirectory获取外部存储设备的路径        Log.i("SDcard路径",Environment.getExternalStorageDirectory().getAbsolutePath());        File file = new File(Environment.getExternalStorageDirectory(),"chenzheng_java.txt");        result+="   sdcard绝对路径:"+Environment.getExternalStorageDirectory().getAbsolutePath();        result+=" /n  sdcard路径:"+Environment.getExternalStorageDirectory().getPath();        if(!file.exists()){        try {file.createNewFile();Log.i("结果", "创建成功。");} catch (IOException e) {Log.i("结果", "创建没成功。");e.printStackTrace();}        }        }                TextView textView = (TextView)findViewById(R.id.textView);        textView.setText(result);            }}

main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    ><TextView  android:id="@+id/textView"    android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="@string/hello"    /></LinearLayout>

AndroidManifest.xml文件代码如下:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"      package="cn.com.android.sdcard"      android:versionCode="1"      android:versionName="1.0">    <uses-sdk android:minSdkVersion="8" />    <!-- 添加对SDCARD的写权限 -->    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>    <application android:icon="@drawable/icon" android:label="@string/app_name">        <activity android:name=".SdCardActivity"                  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卡进行操作,

那么第一,我们首先要有一个SD卡,这里的SD是通过镜像文件模拟的。

第二,我们要在android的manifest.xml文件中进行权限声明,告诉系统,我们的应用具有访问SD卡的权利,<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>.

第三步,我们便可以通过操作文件的代码来进行SD卡的管理了。

---------------------------------------------------------------------------

附录:

向SD卡中写入权限,<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

允许安装和卸载移动设备

,<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS

"></uses-permission>



转载:http://blog.csdn.net/chenzheng_java/article/details/6219335

更多相关文章

  1. Android获取验证码倒计时
  2. Android带进度条文件上传
  3. android 用到的技巧集
  4. android 用到的技巧集
  5. Android获取/清除应用数据
  6. Android之SurfaceView、Camera
  7. android SQLite数据库使用实例
  8. Android(安卓)- LayoutInflater 的使用
  9. android 用到的技巧集

随机推荐

  1. Flutter-基础组件2
  2. android支持多分辨率屏幕
  3. Android(安卓)动画之集合动画AnimatorSet
  4. Retrofit的详解及使用
  5. 错误集录
  6. android 屏幕适配 drawable-hdpi-1920x10
  7. 获取手机(ios,android)的设备唯一码(mac地址
  8. Opencore和stagefright区别
  9. Android(安卓)获取天气http://write.blog
  10. Android编程15个很有用的代码片段