设置俩按钮名字随便起
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="向外置sdcard写共有数据"
android:onClick="onClick" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_alignRight="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="31dp"
android:text="Button"
android:onClick="onClick01"/>
</RelativeLayout>
下面进行代码的实现
package com.example;
import android.os.Bundle;
import android.os.Environment;
import android.os.StatFs;
import android.util.Log;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import android.app.Activity;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View v){
//检测外部sdcard状态
String state=Environment.getExternalStorageState();
Log.i("TAG", "state"+state);
//如果
if(!state.equals(Environment.MEDIA_MOUNTED)||Environment.isExternalStorageRemovable()){
Toast.makeText(this, "请保存sdcard可用", 0).show();
return;
}
//获得外部存储的根目录
File sdcard=Environment.getExternalStorageDirectory();
Log.i("TAG", "sdcard"+sdcard.getPath());
//获得外部存储的子目录
File picDir=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
Log.i("TAG", "picDir"+picDir.getPath());
//获得外置sdcard的可用空间大小
StatFs sf=new StatFs(sdcard.getPath());
/*sf.getBlockCount();快的总数量
sf.getFreeBlocks();空闲块>=可用快
sf.getAvailableBlocks();可用快
sf.getBlockSize();*///块大小
double totalSize=sf.getBlockCount()*sf.getBlockSize()*1.0/1024/1024;
Log.i("TAG", "-------"+totalSize);
String text="hellllllll";
if(text.getBytes().length>sf.getAvailableBlocks()){
Toast.makeText(this, "可用空间不足", 0).show();
return;
}
OutputStream out=null;
try{
out=new FileOutputStream(new File(sdcard,"d1.txt"));
out.write(text.getBytes());
}catch(Exception e){
e.printStackTrace();
}finally{
if(out!=null)
try{out.close();
}catch(Exception e){}
}
}
public void onClick01(View v){
String state=Environment.getExternalStorageState();
Log.i("TAG", "state"+state);
if(!state.equals(Environment.MEDIA_MOUNTED)||Environment.isExternalStorageRemovable()){
Toast.makeText(this, "请保存sdcard可用", 0).show();
return;
}
File sdcarda=getExternalFilesDir("mydic");
StatFs sf=new StatFs(sdcarda.getPath());
String text="hellllllll";
if(text.getBytes().length>sf.getAvailableBlocks()){
Toast.makeText(this, "可用空间不足", 0).show();
return;
}
OutputStream out=null;
try{
out=new FileOutputStream(new File(sdcarda,"d1.txt"));
out.write(text.getBytes());
}catch(Exception e){
e.printStackTrace();
}finally{
if(out!=null)
try{out.close();
}catch(Exception e){}
}
}
}