6.0外置SD卡路径
StorageManager mStorageManager = StorageManager.from(this);
final List<VolumeInfo> vols = mStorageManager.getVolumes();
VolumeInfo sdcardVolume = null ;
for(VolumeInfo vol : vols){
if(vol.getDisk() != null && vol.getDisk().isSd())sdcardVolume = vol;
//如果需要获取usbotg的路径,则改为
// if(vol.getDisk() != null &&vol.getDisk().isUsb()) usbotgVolume = vol;
}
if(sdcardVolume!= null) {
String sdcardPath = sdcardVolume.getPath()==null ? null :sdcardVolume.getPath().toString();
String sdcardState = sdcardVolume.getEnvironmentForState(sdcardVolume.getState());
if(sdcardState.equals(Environment.MEDIA_MOUNTED) && sdcardPath != null){
//外置SD卡已挂载,且路径有效
// sdcardPath 外置SD卡的路径
Toast.makeText(this, sdcardPath, 1000).show();
}
}
5.1 6.0通用获取内置外事SD卡
public String[] getExtSDCardPath() {
StorageManager storageManager = (StorageManager) getSystemService(Context
.STORAGE_SERVICE);
try {
Class<?>[] paramClasses = {};
Method getVolumePathsMethod = StorageManager.class.getMethod("getVolumePaths", paramClasses);
getVolumePathsMethod.setAccessible(true);
Object[] params = {};
Object invoke = getVolumePathsMethod.invoke(storageManager, params);
return (String[])invoke;
} catch (NoSuchMethodException e1) {
e1.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return null;
}
5.1外置SD卡大小
private volatile static Parcelable[] volumes = null;
private Parcelable[] getVolumes() {
if (volumes == null) {
try {
IMountService mountService = IMountService.Stub.asInterface(ServiceManager
.getService("mount"));
volumes = mountService.getVolumeList();
} catch (Exception e) {
Log.e(TAG, "couldn't talk to MountService", e);
}
}
return volumes;
}
public void volumeSizeTest(){
String tips = new String();
Parcelable[] volumes = getVolumes();
for (int i = 0; i < volumes.length; i++){
StorageVolume vol = (StorageVolume)volumes[i];
if( vol.getPath().contains("/storage/sdcard") ){
android.os.StatFs statfs = new android.os.StatFs(vol.getPath());
long nTotalBlocks = statfs.getBlockCount();
long nBlocSize = statfs.getBlockSize();
long nAvailaBlock = statfs.getAvailableBlocks();
long nSDTotalSize = nTotalBlocks * nBlocSize / 1024 / 1024;
long nSDFreeSize = nAvailaBlock * nBlocSize / 1024 / 1024;
tips += vol.getDescription(getApplicationContext()) + "\n\n"
+ getString(R.string.sdcard_totalsize) + nSDTotalSize + "MB" + "\n\n"
+ getString(R.string.sdcard_freesize) + nSDFreeSize + "MB"+"\n\n" ;
}
}
mInfo.setText(tips);
}
public void SDCardSizeTest() {
String sDcString = Environment.getExternalStorageState();
if (sDcString.equals(Environment.MEDIA_MOUNTED)) {
java.io.File pathFile = Environment.getExternalStorageDirectory();
android.os.StatFs statfs = new android.os.StatFs(pathFile.getPath());
long nTotalBlocks = statfs.getBlockCount();
long nBlocSize = statfs.getBlockSize();
long nAvailaBlock = statfs.getAvailableBlocks();
long nSDTotalSize = nTotalBlocks * nBlocSize / 1024 / 1024;
long nSDFreeSize = nAvailaBlock * nBlocSize / 1024 / 1024;
mInfo.setText(getString(R.string.sdcard_tips_success) + "\n\n"
+ getString(R.string.sdcard_totalsize) + nSDTotalSize + "MB" + "\n\n"
+ getString(R.string.sdcard_freesize) + nSDFreeSize + "MB");
} else {
mInfo.setText(getString(R.string.sdcard_tips_failed));
}
}
6.0外置SD卡大小
package com.mediatek.factorymode.sdcard;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import com.mediatek.factorymode.AppDefine;
import com.mediatek.factorymode.R;
import com.mediatek.factorymode.Utils;
import android.os.storage.IMountService;
import android.os.storage.StorageVolume;
import android.util.Log;
import android.os.Parcelable;
import java.io.File;
import android.os.ServiceManager;
import android.os.StatFs;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
public class SDCard extends Activity implements OnClickListener {
private TextView mInfo;
private Button mBtOk;
private Button mBtFailed;
// add by zh
private int uid;
private SharedPreferences mSp;
private static final String TAG = "SDCard";
String path="/storage/sdcard1";
boolean exist= false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sdcard);
mSp = getSharedPreferences("FactoryMode", Context.MODE_PRIVATE);
mInfo = (TextView) findViewById(R.id.sdcard_info);
exist = fileIsExists(path);
if(exist){
Log.i(TAG, "sdcard1 exist");
}else{
Log.i(TAG, "sdcard1 does not exist");
}
doMethod01();
volumeSizeTest(exist);
}
public boolean fileIsExists(String strFile)
{
try
{
File f=new File(strFile);
if(!f.exists())
{
return false;
}
}
catch (Exception e)
{
return false;
}
return true;
}
/**获得当前sdcard的存储状态*/
public void doMethod01(){
//获得当前sdcard的存储状态
String state=Environment.
getExternalStorageState();
//Environment类中提供了
//一些常量用于记录sdcard状态
if(Environment.MEDIA_MOUNTED.
equals(state)){
Log.i("TAG", "sdcard 已经挂载!");
}
}
public void volumeSizeTest(boolean existed){
/* String tips = new String();
Parcelable[] volumes = getVolumes();
for (int i = 0; i < volumes.length; i++){
StorageVolume vol = (StorageVolume)volumes[i];
if( vol.getPath().contains("/storage/sdcard") ){
android.os.StatFs statfs = new android.os.StatFs(vol.getPath());
//获得外部存储sdcard的根目录
File sdcard=Environment.getExternalStorageDirectory();
//获得存储空间大小
StatFs f=new StatFs(sdcard.getPath());
long nTotalBlocks = f.getBlockCount();
long nBlocSize = f.getBlockSize();
long nAvailaBlock = f.getAvailableBlocks();
long nSDTotalSize = nTotalBlocks * nBlocSize / 1024 / 1024;
long nSDFreeSize = nAvailaBlock * nBlocSize / 1024 / 1024;
tips += vol.getDescription(getApplicationContext()) + "\n\n"
+ getString(R.string.sdcard_totalsize) + nSDTotalSize + "MB" + "\n\n"
+ getString(R.string.sdcard_freesize) + nSDFreeSize + "MB"+"\n\n" ;
*/
if(existed){
StatFs f=new StatFs(path);
long blockSize= f.getBlockSizeLong();
long totalSize=f.getBlockCountLong();
long availableBlocks=f.getAvailableBlocksLong();
double sd1=(blockSize*totalSize*1.0)/1024/1024/1024;
double sd2=(blockSize*availableBlocks*1.0)/1024/1024/1024;
String totalSize1 = String.valueOf(sd1);
String str1 = totalSize1.substring(0, 5);
String availableBlocks1 = String.valueOf(sd2);
String str2 = availableBlocks1.substring(0, 5);
mInfo.setText("Available:"+ str2+"GB"+"\n\n"+"Total space:"
+str1+"GB");
}else{
mInfo.setText("NO T-card found!");
}
}
private String getSDCard2MemoryPath() {
return "/storage/sdcard1";
}
private StatFs getStatFs(String path) {
try {
return new StatFs(path);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
网友评论
要引用这个,但是这只能在源码中使用.