android读写文件外存,android外存文件读写

public class MainActivity extends Activity implementsOnClickListener {privateButton fileSave;privateButton fileRead;privateButton picSave;privateButton picRead;privateImageView iv;

@Overrideprotected voidonCreate(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);

}

@Overridepublic voidonClick(View v) {int id =v.getId();switch(id) {caseR.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= newFileOutputStream(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;caseR.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= newFileInputStream(file);byte[] b = new byte[1024];int len = 0;

StringBuilder sb= newStringBuilder();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;caseR.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= newFileOutputStream(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;caseR.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;

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值