当前位置: 编程技术>移动开发
本页文章导读:
▪将TXT文本在内存卡上封存、查看和删除 将TXT文本在内存卡上保存、查看和删除
private EditText title_ed;
private EditText content_ed;
private Button save;
private Button read;
private Button delete;
private TextView tv_title;
private TextView tv_content;
public v.........
▪ 今天使用ViewFlipper的时候发生了一个诡异的有关问题 今天使用ViewFlipper的时候发生了一个诡异的问题。
在横竖屏切换的时候会发生如下错误, 听说是adnroid的bug。 解决方法请参考:http://shaobin0604.iteye.com/blog/966031引用03-10 21:08:15.379: ERROR/Android.........
▪ 查看SDCard的容量状况 查看SDCard的容量情况
private Button myButton;
private TextView myTextView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sd_size);
myButton = (Button) findViewByI.........
[1]将TXT文本在内存卡上封存、查看和删除
来源: 互联网 发布时间: 2014-02-18
将TXT文本在内存卡上保存、查看和删除
private EditText title_ed;
private EditText content_ed;
private Button save;
private Button read;
private Button delete;
private TextView tv_title;
private TextView tv_content;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.save_to_card);
title_ed = (EditText) findViewById(R.id.title);
content_ed = (EditText) findViewById(R.id.content);
save = (Button) findViewById(R.id.button);
read = (Button) findViewById(R.id.read);
delete = (Button) findViewById(R.id.delete);
tv_title = (TextView) findViewById(R.id.tv_title);
tv_content = (TextView) findViewById(R.id.tv_content);
save.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String title = title_ed.getText().toString();
String content = content_ed.getText().toString();
/*
* Environment.getExternalStorageState()方法用于获取SDCard的状态,如果手机
* 装有SDCard,并且可以进行读写,那么方法返回的状态等于Environment.MEDIA_MOUNTED。
*/
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
File sdCardDir = Environment.getExternalStorageDirectory();// 获取SDCard目录
File saveFile = new File(sdCardDir, title + ".txt");
// File saveFile = new File(sdCardDir.getPath() +
// java.io.File.separator+ title + ".txt");
FileOutputStream outStream;
try {
outStream = new FileOutputStream(saveFile);
outStream.write(content.getBytes());
outStream.close();
Toast.makeText(SaveToCard.this, "保存完成",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(SaveToCard.this, "保存不成功",
Toast.LENGTH_LONG).show();
}
}
}
});
read.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String title = title_ed.getText().toString();
File sdCardDir = Environment.getExternalStorageDirectory();// 获取SDCard目录
File file = new File(sdCardDir.getPath() +
java.io.File.separator + title + ".txt");
if(file.exists()){
try {
FileInputStream inStream = new FileInputStream(file);
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length = -1;
while ((length = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, length);
}
outStream.close();
inStream.close();
tv_title.setText(title);
tv_content.setText(outStream.toString());
} catch (IOException e) {
Log.i("FileTest", e.getMessage());
}
}
else{
Toast.makeText(SaveToCard.this, "文件不存在" + sdCardDir.getPath() +
java.io.File.separator + title + ".txt", Toast.LENGTH_LONG)
.show();
}
}
});
delete.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
File sdCardDir = Environment.getExternalStorageDirectory();// 获取SDCard目录
File saveFile = new File(sdCardDir.getPath()
+ java.io.File.separator + title_ed.getText() + ".txt");
if (saveFile.exists()) {
saveFile.delete();
Toast.makeText(SaveToCard.this, "删除成功", Toast.LENGTH_LONG)
.show();
} else {
Toast.makeText(SaveToCard.this, "文件不存在", Toast.LENGTH_LONG)
.show();
}
}
});
}
[2] 今天使用ViewFlipper的时候发生了一个诡异的有关问题
来源: 互联网 发布时间: 2014-02-18
今天使用ViewFlipper的时候发生了一个诡异的问题。
在横竖屏切换的时候会发生如下错误, 听说是adnroid的bug。 解决方法请参考:http://shaobin0604.iteye.com/blog/966031
在横竖屏切换的时候会发生如下错误, 听说是adnroid的bug。 解决方法请参考:http://shaobin0604.iteye.com/blog/966031
引用
03-10 21:08:15.379: ERROR/AndroidRuntime(5143): FATAL EXCEPTION: main
java.lang.IllegalArgumentException: Receiver not registered: android.widget.ViewFlipper$1@40753c10
at android.app.LoadedApk.forgetReceiverDispatcher(LoadedApk.java:610)
at android.app.ContextImpl.unregisterReceiver(ContextImpl.java:823)
java.lang.IllegalArgumentException: Receiver not registered: android.widget.ViewFlipper$1@40753c10
at android.app.LoadedApk.forgetReceiverDispatcher(LoadedApk.java:610)
at android.app.ContextImpl.unregisterReceiver(ContextImpl.java:823)
[3] 查看SDCard的容量状况
来源: 互联网 发布时间: 2014-02-18
查看SDCard的容量情况
private Button myButton;
private TextView myTextView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sd_size);
myButton = (Button) findViewById(R.id.button1);
myTextView = (TextView) findViewById(R.id.textView1);
myButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View arg0) {
showSize();
}
});
}
private void showSize() {
/* 将TextView及ProgressBar设定为空值及0 */
myTextView.setText("");
/* 判断记忆卡是否插入 */
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
/* 取得SD CARD档案路径一般是/sdcard */
File path = Environment.getExternalStorageDirectory();
/* StatFs看文件系统空间使用状况 */
StatFs statFs = new StatFs(path.getPath());
/* Block的size */
long blockSize = statFs.getBlockSize();
/* 总Block数量 */
long totalBlocks = statFs.getBlockCount();
/* 已使用的Block数量 */
long availableBlocks = statFs.getAvailableBlocks();
String[] total = fileSize(totalBlocks * blockSize);
String[] available = fileSize(availableBlocks * blockSize);
/* getMax取得在main.xml里ProgressBar设定的最大值 */
String text = "总共" + total[0] + total[1] + "\n";
text += "可用" + available[0] + available[1];
myTextView.setText(text);
} else if (Environment.getExternalStorageState().equals(
Environment.MEDIA_REMOVED)) {
String text = "SD CARD已移除";
myTextView.setText(text);
}
}
/* 回传为字符串数组[0]为大小[1]为单位KB或MB */
private String[] fileSize(long size) {
String str = "";
if (size >= 1024) {
str = "KB";
size /= 1024;
if (size >= 1024) {
str = "MB";
size /= 1024;
}
}
DecimalFormat formatter = new DecimalFormat();
/* 每3个数字用,分隔如:1,000 */
formatter.setGroupingSize(3);
String result[] = new String[2];
result[0] = formatter.format(size);
result[1] = str;
return result;
}
最新技术文章: