当前位置: 代码迷 >> Android >> android开发累积5-操作sd卡
  详细解决方案

android开发累积5-操作sd卡

热度:8   发布时间:2016-05-01 16:44:46.0
android开发积累5-操作sd卡

android中操作sd卡相关说明:

1、判断sd卡是否存在

if (Environment.getExternalStorageState().equals(
??? ??? ??? ??? Environment.MEDIA_MOUNTED)==false) {
??? ??? ??? text.setText("sd卡不存在");
??? ??? ??? return;
??? ??? }

?

2、获取sd卡根目录

Environment.getExternalStorageDirectory().getPath()

?

3、操作sd卡的权限

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

?

?

下面是一个操作sd卡,创建、删除目录、文件的例子:

CardActivity.java

package Test.wangfu;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.widget.TextView;

public class CardActivity extends Activity {

??? private TextView text = null;
??? private String directory = "/sdcard/wangfu";
??? private String fileName = "/sdcard/wangfu/text.txt";

??? @Override
??? protected void onCreate(Bundle savedInstanceState) {
??? ??? // TODO Auto-generated method stub
??? ??? super.onCreate(savedInstanceState);
??? ??? this.setContentView(R.layout.card);
??? ??? text = (TextView) this.findViewById(R.id.cardLabel);

??? ??? if (Environment.getExternalStorageState().equals(
??? ??? ??? ??? Environment.MEDIA_MOUNTED)==false) {
??? ??? ??? text.setText("sd卡不存在");
??? ??? ??? return;
??? ??? }
??? ??? File path = new File(directory);
??? ??? if (path.exists() == false) {
??? ??? ??? path.mkdir();
??? ??? ??? text.setText("创建sd卡目录完成");
??? ??? ??? // 创建文件
??? ??? ??? try {
??? ??? ??? ??? java.io.OutputStreamWriter fw = new OutputStreamWriter(
??? ??? ??? ??? ??? ??? new FileOutputStream(new File(fileName)), "UTF-8");

??????????????? //写入wangfu writer test
??? ??? ??? ??? fw.write("wangfu writer test");
??? ??? ??? ??? fw.flush();
??? ??? ??? ??? fw.close();
??? ??? ??? } catch (java.lang.Exception ex) {
??? ??? ??? ??? text.setText(ex.getMessage());
??? ??? ??? ??? ex.printStackTrace();
??? ??? ??? }
??? ??? } else {
??? ??? ??? String sContent = "";
??? ??? ??? java.io.InputStreamReader rd;
??? ??? ??? try {
??? ??? ??? ??? rd = new java.io.InputStreamReader(new java.io.FileInputStream(
??? ??? ??? ??? ??? ??? new File(fileName)), "UTF-8");
??? ??? ??? ??? java.io.BufferedReader reader = new java.io.BufferedReader(rd);
??? ??? ??? ??? String s = null;
??? ??? ??? ??? s = reader.readLine();

??? ??? ??? ??? while (s != null) {
??? ??? ??? ??? ??? sContent += s + "\r\n";
??? ??? ??? ??? ??? s = reader.readLine();
??? ??? ??? ??? }
??? ??? ??? ??? reader.close();
??? ??? ??? } catch (java.lang.Exception e) {
??? ??? ??? ???
??? ??? ??? ??? e.printStackTrace();
??? ??? ??? }
??? ??? ??? text.setText(sContent);
??? ??? ??? File file = new File(fileName);
??? ??? ??? file.delete();
??? ??? ??? path.delete();
??? ??? }

??? }

?

card.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
??? android:layout_width="match_parent"
??? android:layout_height="match_parent"
??? android:orientation="vertical" >
???
?<TextView
??????? android:id="@+id/cardLabel"
??????? android:layout_width="fill_parent"
??????? android:layout_height="wrap_content"
??????? android:text="Enter Number to Dial:" />
???
</LinearLayout>

?

?

需要在AndroidManifest.xml添加权限

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

?

  相关解决方案