您的位置:首页 > 移动开发 > Android开发

android外存文件读写

2015-09-24 20:33 357 查看
1.外存txt文件的读写

2.外存图片文件的读写

需要权限:<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

public class MainActivity extends Activity implements OnClickListener {
private Button fileSave;
private Button fileRead;
private Button picSave;
private Button picRead;
private ImageView iv;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fileSave = (Button) findViewById(R.id.button1);
fileRead = (Button) findViewById(R.id.button2);
picSave = (Button) findViewById(R.id.button3);
picRead = (Button) findViewById(R.id.button4);
iv=(ImageView) findViewById(R.id.imageView1);

fileSave.setOnClickListener(this);
fileRead.setOnClickListener(this);
picSave.setOnClickListener(this);
picRead.setOnClickListener(this);
}

@Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.button1:
FileOutputStream saveTxt = null;
// 获取外存储卡状态
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
File dir = Environment.getExternalStorageDirectory();
// 存储一个文本文件
File file = new File(dir, "cc.txt");
try {
saveTxt = new FileOutputStream(file);
saveTxt.write("i am cc!!".getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (saveTxt != null) {
try {
saveTxt.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

}
break;
case R.id.button2:
// 读取外存文件
FileInputStream fis = null;
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
File dir = Environment.getExternalStorageDirectory();
File file = new File(dir, "cc.txt");
try {
fis = new FileInputStream(file);
byte[] b = new byte[1024];
int len = 0;
StringBuilder sb = new StringBuilder();
while ((len = fis.read(b)) != -1) {
String string = new String(b, 0, len);
sb.append(string);
}
Toast.makeText(this, sb.toString(), Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

}
break;
case R.id.button3:
// 把图片存入外存
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
File dir = Environment.getExternalStorageDirectory();
File file = new File(dir, "cc.jpg");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
bm.compress(CompressFormat.JPEG, 100, fos);
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

}

break;
case R.id.button4:
// 从外存读取图片
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
File dir = Environment.getExternalStorageDirectory();
File file = new File(dir, "cc.jpg");
Bitmap bm=BitmapFactory.decodeFile(file.getPath());
iv.setImageBitmap(bm);
}
break;
default:
break;
}
}

}


布局文件:

<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="com.example.sdcardfileio.MainActivity" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginLeft="24dp"
android:text="Button" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:text="Button" />

<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button2"
android:layout_below="@+id/button2"
android:text="Button" />

<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button3"
android:layout_below="@+id/button3"
android:text="Button" />

<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button4"
android:layout_marginTop="70dp"
android:layout_toRightOf="@+id/textView1"
/>

</RelativeLayout>


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: