android读写Sdcard

来源:互联网 发布:京东秒杀软件安卓版 编辑:程序博客网 时间:2024/05/01 17:13

android配置文件

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.androidsdk"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="10" />    <!--添加读写sdcard的授权  -->    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>    <!--测试 -->    <instrumentation android:name="android.test.InstrumentationTestRunner"         android:targetPackage="com.example.androidsdk">            </instrumentation>    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <!--添加测试  -->        <uses-library android:name="android.test.runner"/>        <activity            android:name="com.example.androidsdk.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>

下面是读写的方法类

package com.example.androidsdk;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import android.content.Context;import android.os.Environment;import android.util.Log;public class FileService {private Context context;public FileService(Context context){this.context=context;}public FileService(){}public boolean saveContentToSdcard(String fileName,String content){boolean flag=false;FileOutputStream fileOutputStream=null;//获取sdcard卡所在的路径File file=new File(Environment.getExternalStorageDirectory(), fileName);//判断sdcard卡是否可用    if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){        try {fileOutputStream=new FileOutputStream(file);    fileOutputStream.write(content.getBytes());    flag=true;    } catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{if(fileOutputStream!=null){try {fileOutputStream.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}        }return flag;}//读取public String getFileFromSdcard(String filename){FileInputStream inputStream=null;//缓存的流,和磁盘无关,不需要关闭ByteArrayOutputStream outputStream=new ByteArrayOutputStream();File file=new File(Environment.getExternalStorageDirectory(),filename);if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){try {inputStream=new FileInputStream(file);    int len=0;    byte[] data=new byte[1024];    while((len=inputStream.read(data))!=-1){}{    outputStream.write(data,0,len);    }    } catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{if(inputStream!=null){try {inputStream.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}return new String(outputStream.toByteArray());}}

测试方法

package com.example.androidsdk;import android.content.Context;import android.test.AndroidTestCase;import android.util.Log;public class MyTest extends AndroidTestCase {   private final String TAG="MyTest";  public void saveFile(){  Context context=getContext();FileService fileService=new FileService(context);boolean flag=fileService.saveContentToSdcard("hello.txt", "111张航");Log.i(TAG,"-->>"+flag);}public void readFile(){Context context=getContext();FileService fileService=new FileService(context);String read=fileService.getFileFromSdcard("hello.txt");Log.i(TAG, "-->>"+read);}}


原创粉丝点击