设为首页 加入收藏

TOP

Android 自动创建文件到scard卡并保存(一)
2014-11-24 02:01:50 来源: 作者: 【 】 浏览:0
Tags:Android 自动 创建 文件 scard 卡并 保存

直接上Activity代码:


public class DRMPTestActivity extends Activity implements OnClickListener{

String fileName = "";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

}



/* (non-Javadoc)
* @see android.view.View.OnClickListener#onClick(android.view.View)
*/
@Override
public void onClick(View v) {

// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.save:
Time t = new Time();
t.setToNow();
//定义文件名称
fileName = "DRMP-" + t.year + t.month + t.monthDay
+ t.hour + t.minute + ".txt";
final EditText fileText = new EditText(DRMPTestActivity.this);
fileText.setText(fileName);
// 定义一个对话框
new AlertDialog.Builder(DRMPTestActivity.this)
.setTitle("请输入文件名")// 标题
.setIcon(android.R.drawable.ic_dialog_info)// 图标
.setView(fileText)// 为对话框传入了一个文本编辑框
.setPositiveButton("确定",
// 对话框监听事件
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
String temp = fileText.getText().toString();
if (temp != null || temp != ""){
fileName = temp;
}
String data = “哈回家啊就是开始卡”;

WriteData(fileName, data);
Toast.makeText(getApplicationContext(), "\""+fileName+"\"正在保存...", Toast.LENGTH_LONG)
.show();
}
}).setNegativeButton("取消", null).show();

break;
}

}

// 将数据存储到SD卡,filename为文件名,data为要存储的内容
private void WriteData(String fileName, String data) {
FileService fileService = new FileService(getApplicationContext());
try {
// 判断SDCard是否存在并且可以读写
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
fileService.saveToSD(fileName, data);
} else {

}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}


}


fileService代码:


package com.cmdi.util;


import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;


import android.content.Context;


public class FileService {


private Context context;


public FileService(Context context) {
super();
this.context = context;
}


/**
* 写入文件到SD卡
*
* @throws IOException
*/
public void saveToSD(String fileNameStr, String fileContentStr)
throws IOException {


// 备注:Java File类能够创建文件或者文件夹,但是不能两个一起创建
File file = new File("/mnt/sdcard/DRMP");


if (!file.exists()) {
file.mkdir();
}
File file1 = new File(file, fileNameStr);


FileOutputStream fos = new FileOutputStream(file1,true);
fos.write(fileContentStr.getBytes());
fos.close();


}


/**
* 保存文件到手机
*
* @param fileNameStr
* 文件名
* @param fileContentStr
* 文件内容
* @throws IOException
*/
public void save(String fileNameStr, String fileContentStr)
throws IOException {
// 私有操作模式:创建出来的文件只能被本应用访问,其它应用无法访问该文件,另外采用私有操作模式创建的文件,写入文件中的内容会覆盖原文件的内容
FileOutputStream fos = context.openFileOutput(fileNameStr,
context.MODE_PRIVATE);
fos.write(fileContentStr.getBytes());
fos.close();
}


public void saveAppend(String fileNameStr, String fileContentStr)
throws IOException {
// 追加操作模式:不覆盖源文件,但是同样其它应用无法访问该文件
FileOutputStream fos = context.openFileOutput(fileNameStr,
context.MODE_APPEND);
fos.write(fileContentStr.getBytes());
fos.close();
}


publ

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Java事务处理 下一篇Tiny6410内核模块编译之HelloWorld

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: