android操作sdcard
16lz
2021-12-04
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
更多相关文章
- Android获取验证码倒计时
- Android带进度条文件上传
- android 用到的技巧集
- android 用到的技巧集
- Android获取/清除应用数据
- Android之SurfaceView、Camera
- android SQLite数据库使用实例
- Android(安卓)- LayoutInflater 的使用
- android 用到的技巧集