下面列出了android.os.Environment#getExternalStorageState ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Determine if SD card exists.
*
* @return T=exists, F=not found
*/
public static boolean testSaveLocationExists() {
String sDCardStatus = Environment.getExternalStorageState();
boolean status;
// If SD card is mounted
if (sDCardStatus.equals(Environment.MEDIA_MOUNTED)) {
status = true;
}
// If no SD card
else {
status = false;
}
return status;
}
/**
* 创建一条图片地址uri,用于保存拍照后的照片
*
* @param context
* @param suffixType
* @return 图片的uri
*/
@Nullable
public static Uri createImageUri(final Context context, String suffixType) {
final Uri[] imageFilePath = {null};
String status = Environment.getExternalStorageState();
String time = ValueOf.toString(System.currentTimeMillis());
// ContentValues是我们希望这条记录被创建时包含的数据信息
ContentValues values = new ContentValues(3);
values.put(MediaStore.Images.Media.DISPLAY_NAME, DateUtils.getCreateFileName("IMG_"));
values.put(MediaStore.Images.Media.DATE_TAKEN, time);
values.put(MediaStore.Images.Media.MIME_TYPE, TextUtils.isEmpty(suffixType) ? PictureMimeType.MIME_TYPE_IMAGE : suffixType);
// 判断是否有SD卡,优先使用SD卡存储,当没有SD卡时使用手机存储
if (status.equals(Environment.MEDIA_MOUNTED)) {
values.put(MediaStore.Images.Media.RELATIVE_PATH, PictureMimeType.DCIM);
imageFilePath[0] = context.getContentResolver()
.insert(MediaStore.Images.Media.getContentUri("external"), values);
} else {
imageFilePath[0] = context.getContentResolver()
.insert(MediaStore.Images.Media.getContentUri("internal"), values);
}
return imageFilePath[0];
}
/**
* 拍照
* @author leibing
* @createTime 2017/5/16
* @lastModify 2017/5/16
* @param
* @return
*/
private void takePhotos() {
if (connectStatus == PROMPT_LINE_TIP_ONE_STATUS
|| connectStatus == PROMPT_LINE_TIP_THREE_STATUS
|| connectStatus == PROMPT_LINE_TIP_FOUR_STATUS){
ToastUtils.show(JkChatActivity.this, JK_CHAT_DISCONNECT_TIP);
return;
}
// 判断SD卡是否存在
String SDState = Environment.getExternalStorageState();
if (SDState.equals(Environment.MEDIA_MOUNTED)) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
ContentValues values = new ContentValues();
// 使用照相机拍照,拍照后的图片会存放在相册中。使用这种方式好处就是:获取的图片是拍照后的原图,
// 如果不实用ContentValues存放照片路径的话,拍照后获取的图片为缩略图有可能不清晰
photoUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, photoUri);
startActivityForResult(intent, TAKE_PHOTO_CODE);
} else {
Toast.makeText(this, SDCARD_NO_EXIST, Toast.LENGTH_LONG).show();
}
}
/**
* 获取SD卡信息
*
* @return
*/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public static SDCardInfo getSDCardInfo() {
SDCardInfo sd = new SDCardInfo();
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
sd.isExist = true;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
File sdcardDir = Environment.getExternalStorageDirectory();
StatFs sf = new StatFs(sdcardDir.getPath());
sd.totalBlocks = sf.getBlockCountLong();
sd.blockByteSize = sf.getBlockSizeLong();
sd.availableBlocks = sf.getAvailableBlocksLong();
sd.availableBytes = sf.getAvailableBytes();
sd.freeBlocks = sf.getFreeBlocksLong();
sd.freeBytes = sf.getFreeBytes();
sd.totalBytes = sf.getTotalBytes();
}
}
LogUtils.i(TAG, sd.toString());
return sd;
}
/**
* @return true if External Storage Present
*/
private static boolean isExternalAvailable() {
boolean externalStorageAvailable = false;
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
// We can read and write the media
externalStorageAvailable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// We can only read the media
externalStorageAvailable = true;
} else {
// Something else is wrong. It may be one of many other states, but all we need
// to know is we can neither read nor write
externalStorageAvailable = false;
}
return externalStorageAvailable;
}
/**
* Check if external storage is readable or not
* @return true if readable, false otherwise
*/
public static boolean isExternalStorageReadable() {
String state = Environment.getExternalStorageState();
return Environment.MEDIA_MOUNTED.equals(state) ||
Environment.MEDIA_MOUNTED_READ_ONLY.equals(state);
}
public static boolean existSDcard() {
String externalStorageState;
try {
externalStorageState = Environment.getExternalStorageState();
} catch (NullPointerException e) { // (sh)it happens (Issue #660)
externalStorageState = "";
}
return MEDIA_MOUNTED.equals(externalStorageState);
}
public static boolean hasStorage(boolean requireWriteAccess) {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
if (requireWriteAccess) {
boolean writable = checkFsWritable();
return writable;
} else {
return true;
}
} else if (!requireWriteAccess && Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
return true;
}
return false;
}
public static boolean hasSpaceForSize(long size) {
String state = Environment.getExternalStorageState();
if (!Environment.MEDIA_MOUNTED.equals(state)) {
return false;
}
String path = Environment.getExternalStorageDirectory().getPath();
try {
StatFs stat = new StatFs(path);
return stat.getAvailableBlocks() * (long) stat.getBlockSize() > size;
} catch (Exception e) {
Log.i(TAG, "Fail to access external storage", e);
}
return false;
}
private static boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
}
return false;
}
/**
* 获取插件安装目录,内置目录或者SD卡目录
* 目前VR插件是安装在SD的/Android/data目录的
*/
private static File getPreferredInstallLocation(Context context, PackageInfo pkgInfo, String apkName) {
int installLocation = ReflectionUtils.on(pkgInfo).<Integer>get("installLocation");
PluginDebugLog.installFormatLog(TAG, "plugin apk %s, installLocation: %s,", apkName, installLocation);
// see PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL
final int INSTALL_LOCATION_PREFER_EXTERNAL = 2;
boolean preferExternal = installLocation == INSTALL_LOCATION_PREFER_EXTERNAL;
// 查看外部存储器是否可用
if (preferExternal) {
String state = Environment.getExternalStorageState();
if (!Environment.MEDIA_MOUNTED.equals(state)) { // 不可用
preferExternal = false;
}
}
if (preferExternal) {
// 尝试安装到外部存储器
File externalDir = context.getExternalFilesDir(PluginInstaller.PLUGIN_ROOT_PATH);
if (externalDir != null && externalDir.exists()) {
File destFile = new File(externalDir, apkName);
PluginDebugLog.installFormatLog(TAG, "install to Location %s", destFile.getPath());
return destFile;
}
}
// 返回默认安装位置
return getDefaultInstallLocation(context, apkName);
}
/**
* @return True if the external storage is writable. False otherwise.
*/
public static boolean isWritable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
}
return false;
}
public static boolean a()
{
String s = Environment.getExternalStorageState();
return "mounted".equals(s) || "mounted_ro".equals(s);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.action_edit_comment:
showCommentDialog((KanboardComment)commentListview.getAdapter().getItem(info.position));
return true;
case R.id.action_delete_comment:
showDeleteCommentDialog((KanboardComment)commentListview.getAdapter().getItem(info.position));
return true;
case R.id.action_edit_subtask:
showSubtaskDialog((KanboardSubtask)subtaskListview.getAdapter().getItem(info.position));
return super.onContextItemSelected(item);
case R.id.action_delete_subtask:
showDeleteSubtaskDialog((KanboardSubtask)subtaskListview.getAdapter().getItem(info.position));
return true;
case R.id.action_download_file:
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
downloadFileId = ((KanboardTaskFile) filesListview.getAdapter().getItem(info.position)).getId();
downloadFileName = ((KanboardTaskFile) filesListview.getAdapter().getItem(info.position)).getName();
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
Intent downloadIntent = new Intent(this, DownloadIntentService.class);
downloadIntent.putExtra("request", KanboardRequest.downloadTaskFile(downloadFileId).JSON[0]);
downloadIntent.putExtra("filename", downloadFileName);
startService(downloadIntent);
} else {
ActivityCompat.requestPermissions((Activity) self,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
Constants.RequestStoragePermission);
}
} else {
Snackbar.make(findViewById(R.id.root_layout), getString(R.string.error_no_sdcard), Snackbar.LENGTH_LONG).show();
}
return true;
case R.id.action_delete_file:
showDeleteTaskFileDialog((KanboardTaskFile) filesListview.getAdapter().getItem(info.position));
return true;
default:
return super.onContextItemSelected(item);
}
}
public static boolean sdCardMounted() {
String state = Environment.getExternalStorageState();
return state.equals("mounted") && !state.equals("mounted_ro");
}
protected boolean isExternalStorageReadable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state))
return true;
return false;
}
public static boolean isExternalStorageReadable() {
String state = Environment.getExternalStorageState();
return Environment.MEDIA_MOUNTED.equals(state) ||
Environment.MEDIA_MOUNTED_READ_ONLY.equals(state);
}
public boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
return Environment.MEDIA_MOUNTED.equals(state);
}
/**
* @return True if the primary shared storage is writable. False otherwise.
* @deprecated As of 6.1.7, will be removed in the future.
*/
@Deprecated
public static boolean isWritable() {
String state = Environment.getExternalStorageState();
return Environment.MEDIA_MOUNTED.equals(state);
}
private static boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
return Environment.MEDIA_MOUNTED.equals(state);
}