您的位置:首页 > 其它

data_SDCard_ReadAndWrite

2015-11-03 20:45 260 查看
MainActivity.java

package com.example.hd.storage_sdcard;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
private Button mButton;
private Button mButton2;
Storage_SDCardService sdCardSave = new Storage_SDCardService(MainActivity.this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = (Button) findViewById(R.id.button1);
mButton2 = (Button) findViewById(R.id.button2);
mButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
boolean flag = sdCardSave.saveToSDCard("jjjj.txt", "fjsklfjaljdfkl".getBytes());
Log.i(TAG, flag+"");
}
});
mButton2.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String read = sdCardSave.readToSDCard("jjjj.txt");
Log.i(TAG, read);
}
});
}

}


Storage_SDCardService.java

package com.example.hd.storage_sdcard;

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 Storage_SDCardService {
private static final String TAG = "Storage_SDCardService";
Context context;
String state = Environment.getExternalStorageState();
File root = Environment.getExternalStorageDirectory();

public Storage_SDCardService(Context context) {
// TODO Auto-generated constructor stub
this.context = context;
}

public boolean saveToSDCard(String fileName, byte[] data) {
boolean flag = false;

if (state.equals(Environment.MEDIA_MOUNTED)) {

Log.i(TAG, root.getAbsolutePath());
try {
File file = new File(root.getAbsoluteFile() + "/txt");
if (!file.exists()) {
file.mkdir();
FileOutputStream outputStream = new FileOutputStream(
new File(file, fileName));
outputStream.write(data, 0, data.length);
outputStream.close();
return true;
}

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return flag;
}

public String readToSDCard(String fileName) {
FileInputStream inputStream = null;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
if (state.equals(Environment.MEDIA_MOUNTED)) {
File file = new File(root.getAbsolutePath()+"/txt/"+fileName);
int len = 0;
byte[] data = new byte[1024];
if(file.exists()){
try {
inputStream = new FileInputStream(file);
while ((len = inputStream.read(data)) != -1) {
outputStream.write(data, 0, len);
}
return new String(outputStream.toByteArray());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return null;
}

}


AndroidMainfest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hd.storage_sdcard"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="17"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: