1.sd的注册
@Override
protected void onStart() {
super.onStart();
//注册sd卡插拔广播
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_MEDIA_MOUNTED);
filter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
filter.addAction(Intent.ACTION_MEDIA_REMOVED);
filter.setPriority(1000);
filter.addDataScheme("file");
registerReceiver(mReceiver, filter);
}
2.获取内置存储和外部存储信息
//0-内置 1-外置
public static String[] getAllSdPaths(Context context) {
Method mMethodGetPaths = null;
String[] paths = null;
StorageManager mStorageManager = (StorageManager)context
.getSystemService(context.STORAGE_SERVICE);
try {
mMethodGetPaths = mStorageManager.getClass().getMethod("getVolumePaths");
paths = (String[]) mMethodGetPaths.invoke(mStorageManager);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return paths;
}
3.权限
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
4.判断SD卡是否存在
public static final String DCIM = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DCIM).toString();
String sMountPoint = StorageManagerEx.getDefaultPath();
//判断SD卡是否存在
private boolean ExistSDCard() {
if (DCIM.equals(sMountPoint+FOLDER_PATH)) {
return false;
} else{
return true;
}
}
5.SD卡热插拔布局更新
//sd插拔布局更新
private BroadcastReceiver mReceiver=new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(Intent.ACTION_MEDIA_MOUNTED)){
refresh();
}else if(intent.getAction().equals(Intent.ACTION_MEDIA_UNMOUNTED)){
refresh();
}
}
};
//刷新View
private void refresh(){
FOLDER_PATH = "/" + Environment.DIRECTORY_DCIM;
sMountPoint = StorageManagerEx.getDefaultPath();
if(ExistSDCard()){
linearLayout_sdStorage.setVisibility(View.VISIBLE);
}else{
linearLayout_sdStorage.setVisibility(View.GONE);
}
}
6.开发中遇到的问题
//系统第一次开机或是恢复出厂设置识别不了SD卡状态
import android.os.storage.StorageVolume;
import android.os.storage.StorageManager;
import android.content.Context;
StorageManager mStorageManager = (StorageManager)mainActivity.getSystemService(Context.STORAGE_SERVICE);
StorageVolume[] volumes = mStorageManager.getVolumeList();
for (int i = 0; i < volumes.length; i++) {
LogUtils.d("sdcard test", "volumes[" + i + "]=" + volumes[i]);
if (volumes[i].isRemovable()){
// removable sd card : ext sd card
mSdCard2Path = volumes[i].getPath();
break;
}
}
if(!"".equals(mSdCard2Path)){
String FOLDER_PATH = "/" + Environment.DIRECTORY_DCIM + "/Camera";
filepath = mSdCard2Path + FOLDER_PATH;
}
//系统Camera2拍照修改路径后不能直接进行SD存储
vendor/mediatek/proprietary/packages/apps/Camera2/common/src/com/mediatek/camera/common/storage/Storage.java
private boolean isDefaultPathCanUsed(Context context) {
......
File dir = null;
if (sGetDefaultPath != null) {
try {
//sMountPoint = StorageManagerExt.getDefaultPath();
//sd卡状态为true 第一次开机或是恢复出厂设置不能识别保存文件
StorageManager mStorageManager = (StorageManager)context.getSystemService(Context.STORAGE_SERVICE);
StorageVolume[] volumes = mStorageManager.getVolumeList();
for (int i = 0; i < volumes.length; i++) {
LogHelper.d(TAG, "volumes[" + i + "]=" + volumes[i]);
if (volumes[i].isRemovable()){
sMountPoint = volumes[i].getPath();
break;
}
}
if(!"".equals(sMountPoint)){
dir = new File(sMountPoint + FOLDER_PATH);
if(!dir.exists()){
dir.mkdirs();
}
}
boolean isDirectory = dir.isDirectory();
boolean canWrite = dir.canWrite();
if (!isDirectory || !canWrite) {
mIsDefaultPathCanUsed = false;
} else {
mIsDefaultPathCanUsed = true;
}
} catch (Exception e) {
e.printStackTrace();
mIsDefaultPathCanUsed = false;
}
}
return mIsDefaultPathCanUsed;
}