下面列出了android.os.Environment#MEDIA_MOUNTED 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static boolean isStorageWritable() {
boolean isExternalStorageAvailable;
boolean isExternalStorageWritable;
String state = Environment.getExternalStorageState();
switch (state) {
case Environment.MEDIA_MOUNTED:
isExternalStorageAvailable = true;
isExternalStorageWritable = true;
break;
case Environment.MEDIA_MOUNTED_READ_ONLY:
isExternalStorageAvailable = true;
isExternalStorageWritable = false;
break;
default:
isExternalStorageAvailable = false;
isExternalStorageWritable = false;
break;
}
return isExternalStorageAvailable && isExternalStorageWritable;
}
private void initiImageLoader() {
DisplayImageOptions options = new DisplayImageOptions.Builder()
.bitmapConfig(Bitmap.Config.RGB_565)
.imageScaleType(ImageScaleType.EXACTLY)
.cacheOnDisc(true)
.displayer(new FadeInBitmapDisplayer(200))
.showImageOnLoading(R.drawable.ic_launcher)
.build();
File cacheDir;
if(Environment.getExternalStorageState() == Environment.MEDIA_MOUNTED){
cacheDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
}else{
cacheDir = getCacheDir();
}
ImageLoaderConfiguration.Builder configBuilder = new ImageLoaderConfiguration.Builder(mContext)
.threadPoolSize(2)
.memoryCache(new WeakMemoryCache())
.denyCacheImageMultipleSizesInMemory()
.discCache(new UnlimitedDiscCache(cacheDir))
.defaultDisplayImageOptions(options);
if(BuildConfig.DEBUG){
configBuilder.writeDebugLogs();
}
ImageLoader.getInstance().init(configBuilder.build());
}
public static File getDiskCacheDir(final Context context, final String uniqueName) {
// Check if media is mounted or storage is built-in, if so, try and use external cache dir
// otherwise use internal cache dir
final boolean externalCacheAvailable = (Environment.getExternalStorageState() == Environment.MEDIA_MOUNTED) ||
!Utils.isExternalStorageRemovable();
String cachePath = null;
if (externalCacheAvailable && Utils.getExternalCacheDir(context) != null) {
cachePath = Utils.getExternalCacheDir(context).getPath();
} else {
cachePath = context.getCacheDir().getPath();
}
return new File(cachePath + File.separator + uniqueName);
}
private static File prepareTargetDirectory() throws DownloadFailException, IOException {
File downloadFolder =
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
if (
downloadFolder
!= null
&& (downloadFolder.exists() || downloadFolder.mkdir() || downloadFolder.createNewFile())
&& downloadFolder.canWrite()
) {
return downloadFolder;
}
File externalStorage = Environment.getExternalStorageDirectory();
String externalStorageState = Environment.getExternalStorageState();
if (
externalStorage
!= null
&& (externalStorage.exists() || externalStorage.mkdir() || externalStorage.createNewFile())
&& externalStorage.canWrite()
&& externalStorageState.equals(Environment.MEDIA_MOUNTED)
) {
return new File(externalStorage, "Download");
}
File appExternal = LMvdApp.getInstance().getExternalFilesDir(null);
if (
appExternal
!= null
&& (appExternal.exists() || appExternal.mkdir() || appExternal.createNewFile())
&& appExternal.canWrite()
) {
return new File(appExternal, "Download");
}
String message;
switch (externalStorageState) {
case Environment.MEDIA_UNMOUNTABLE:
message = "External storage is un-mountable.";
break;
case Environment.MEDIA_SHARED:
message = "USB mass storage is turned on. Can not mount external storage.";
break;
case Environment.MEDIA_UNMOUNTED:
message = "External storage is not mounted.";
break;
case Environment.MEDIA_MOUNTED_READ_ONLY:
message = "External storage is mounted but has no write access.";
break;
case Environment.MEDIA_BAD_REMOVAL:
message = "External storage was removed without being properly ejected.";
break;
case Environment.MEDIA_REMOVED:
message = "External storage does not exist. Probably removed.";
break;
case Environment.MEDIA_NOFS:
message = "External storage is blank or has unsupported filesystem.";
break;
case Environment.MEDIA_CHECKING:
message = "Still checking for external storage.";
break;
case Environment.MEDIA_EJECTING:
message = "External storage is currently being ejected.";
break;
case Environment.MEDIA_UNKNOWN:
message = "External storage is not available for some unknown reason.";
break;
case Environment.MEDIA_MOUNTED:
message = "External storage is mounted but for some unknown reason is not" +
" available.";
break;
default:
message = "External storage is not available. No reason.";
}
throw new DownloadFailException(message);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.set_path_button:
mFontTextView.setFontPath("fonts/my_font.ttf");
break;
case R.id.create_button:
v.setEnabled(false); // can click only once
ViewGroup parent = (ViewGroup) findViewById(R.id.container_layout);
createFontTextView(parent, "fonts/my_font.ttf");
// createTextView(parent, "fonts/my_font.ttf");
break;
case R.id.replace_font_from_asset_btn:
/* Method 3: replace system default font */
FontUtils.getInstance().replaceSystemDefaultFontFromAsset(this, "fonts/my_font.ttf");
recreate();
break;
case R.id.replace_font_from_file_btn:
if (Environment.getExternalStorageState() == Environment.MEDIA_MOUNTED) {
String fontPath = Environment.getExternalStorageDirectory() + "/whinc/my_font.ttf";
FontUtils.getInstance().replaceSystemDefaultFontFromFile(this, fontPath);
recreate();
} else {
Toast.makeText(this, "External storage is not accessible!", Toast.LENGTH_SHORT).show();
}
break;
case R.id.add_view_btn:
v.setEnabled(false); // can click only once
Button button = new Button(this, null, R.attr.buttonStyle);
button.setText("Button");
RelativeLayout layout = (RelativeLayout) findViewById(R.id.font_text_layout);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
);
lp.addRule(RelativeLayout.CENTER_IN_PARENT);
layout.addView(button, lp);
break;
default:
break;
}
}
/**
* 判断sdcard是否已经挂载
* @param context
* @return
*/
public static boolean hasSdcard(Context context){
return Environment.getExternalStorageState() == Environment.MEDIA_MOUNTED;
}
/**
* 判断sdcard是否已经挂载
* @param context
* @return
*/
public static boolean hasSdcard(Context context){
return Environment.getExternalStorageState() == Environment.MEDIA_MOUNTED;
}