package com.example.qq_logindemo; import android.Manifest; import android.app.Activity; import android.content.pm.PackageManager; import android.os.Build; import android.os.Bundle; import android.os.Environment; import android.text.format.Formatter; import android.util.Log; import android.view.View; import android.widget.Button; import androidx.annotation.Nullable; import java.io.File; import java.io.FileOutputStream; public class SDCardDemoActivity extends Activity implements View.OnClickListener { private Button writeDataBtn; private static final String TAG="SDCardDemoActivity"; private Button checkSDCardBtn; private Button getFreeSDCardBtn; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sd_card); if (Build.VERSION.SDK_INT>=23) { int REQUEST_CODE_CONTACT = 101; String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE}; //验证是否许可权限 for (String str : permissions) { if (this.checkSelfPermission(str) != PackageManager.PERMISSION_GRANTED) { //申请权限 this.requestPermissions(permissions, REQUEST_CODE_CONTACT); return; } } } initView(); initOnClickListener(); } private void initOnClickListener() { writeDataBtn.setOnClickListener(this); checkSDCardBtn.setOnClickListener(this); getFreeSDCardBtn.setOnClickListener(this); } private void initView() { getFreeSDCardBtn = this.findViewById(R.id.btn_get_free_sd_card); writeDataBtn = this.findViewById(R.id.btn_write_2_sd_card); checkSDCardBtn = this.findViewById(R.id.btn_check_sd_card); } @Override public void onClick(View v) { if (v==writeDataBtn) { //存储到SD卡 File externalStorageDirectory = Environment.getExternalStorageDirectory(); Log.d(TAG, "已经存储到 ---> "+externalStorageDirectory.toString()); File filePath =new File("/storage/self/primary"); File file =new File(filePath,"info.txt"); try { FileOutputStream fos=new FileOutputStream(file); fos.write("heihei".getBytes()); fos.close(); } catch (Exception e) { e.printStackTrace(); } } else if (v == checkSDCardBtn) { //检查SD卡是否挂载 String state = Environment.getExternalStorageState(); if(state.equals(Environment.MEDIA_MOUNTED)){ Log.d(TAG, "SD卡已经挂载"); }else if (state.equals(Environment.MEDIA_UNMOUNTED)){ Log.d(TAG, " SD卡已经删除"); } } else if(v==getFreeSDCardBtn){ File exFile = Environment.getExternalStorageDirectory(); Log.d(TAG, "onClick: File Path ==="+exFile.toString()); long freeSpace =exFile.getFreeSpace(); //转换单位,把long转化成直观的 kb,Gb等 String freeText= Formatter.formatFileSize(this,freeSpace); Log.d(TAG, "onClick: SD卡空余空间为:"+freeText); } } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/btn_write_2_sd_card" android:layout_width="match_parent" android:text="存储到SD卡" android:layout_height="wrap_content"/> <Button android:id="@+id/btn_check_sd_card" android:layout_width="match_parent" android:text="检查SD卡是否挂载" android:layout_height="wrap_content"/> <Button android:id="@+id/btn_get_free_sd_card" android:layout_width="match_parent" android:text="SD卡剩余空间" android:layout_height="wrap_content"/> </LinearLayout>
原文:https://www.cnblogs.com/suanai/p/12260804.html