public void download(String url, String fileName) { if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) { Uri uri = Uri.parse(url); DownloadManager.Request r = new DownloadManager.Request(uri); r.setAllowedNetworkTypes(Request.NETWORK_WIFI); r.setAllowedOverRoaming(false); // set mime type MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton(); String mimeType = mimeTypeMap.getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(url)); r.setMimeType(mimeType); // set in notification r.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE); r.setVisibleInDownloadsUi(true); // sdcard if (TextUtils.isEmpty(fileName)) fileName = uri.getLastPathSegment(); r.setDestinationInExternalFilesDir(mContext, File.separator, fileName); r.setTitle(fileName); // start download mDownloadId = mDM.enqueue(r); } else if (mListener != null) { SystemClock.sleep(1000); mListener.onDownloadComplete(null); } }
@Override public void checkMemoryCard() { if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) { AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle("提示"); builder.setMessage("请检查内存卡"); builder .setPositiveButton( "设置", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); Intent intent = new Intent(Settings.ACTION_SETTINGS); context.startActivity(intent); } }) .setNegativeButton( "退出", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); csdnApplication.exit(); } }); Dialog dialog = builder.create(); dialog.show(); } }
@Override protected Integer doInBackground(Void... params) { if (isCancelled()) return STATUS_CANCELLED; if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) { return STATUS_NO_STORAGE; } final GestureLibrary store = sStore; if (store.load()) { for (String name : store.getGestureEntries()) { if (isCancelled()) break; for (Gesture gesture : store.getGestures(name)) { final Bitmap bitmap = gesture.toBitmap(mThumbnailSize, mThumbnailSize, mThumbnailInset, mPathColor); final NamedGesture namedGesture = new NamedGesture(); namedGesture.gesture = gesture; namedGesture.name = name; mAdapter.addBitmap(namedGesture.gesture.getID(), bitmap); publishProgress(namedGesture); } } return STATUS_SUCCESS; } return STATUS_NOT_LOADED; }
/** * Check the external storage status * * @return */ private boolean isExternalStorageAvilable() { String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state)) { return true; } return false; }
// 在onStartCommand()方法中准备相关的下载工作: @Override public int onStartCommand(Intent intent, int flags, int startId) { appName_en = getResources().getString(R.string.app_name_en); appName_cn = getResources().getString(R.string.app_name); // 获取传值 appDownLoadURL = intent.getStringExtra(ConstantStrUtil.STR_APPDOWNLOADURL); // 创建文件 if (android.os.Environment.MEDIA_MOUNTED.equals( android.os.Environment.getExternalStorageState())) { updateDir = new File(Environment.getExternalStorageDirectory(), ""); updateFile = new File(updateDir.getPath(), appName_en + ".apk"); } this.updateNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); this.updateNotification = new Notification(); // 设置下载过程中,点击通知栏,回到主界面 updateIntent = new Intent(this, AppStart.class); updatePendingIntent = PendingIntent.getActivity(this, 0, updateIntent, 0); // 设置通知栏显示内容 updateNotification.icon = R.drawable.logo1; updateNotification.tickerText = "开始下载"; updateNotification.setLatestEventInfo(this, appName_cn, "0%", updatePendingIntent); // 发出通知 updateNotificationManager.notify(0, updateNotification); // 开启一个新的线程下载,如果使用Service同步下载,会导致ANR问题,Service本身也会阻塞 new Thread(new updateRunnable()).start(); // 这个是下载的重点,是下载的过程 return super.onStartCommand(intent, flags, startId); }
private void openIfRequired() throws IOException { String state = Environment.getExternalStorageState(); if (mSerializer == null) { String fileName = mReportFile; if (mMultiFile) { fileName = fileName.replace("$(suite)", "test"); } if (mReportDir == null) { if (Environment.MEDIA_MOUNTED.equals(state) && !Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { File f = mContext.getExternalFilesDir("testng"); mOutputStream = new FileOutputStream(new File(f, fileName)); } else { mOutputStream = mTargetContext.openFileOutput(fileName, 0); } } else { mOutputStream = new FileOutputStream(new File(mReportDir, fileName)); } mSerializer = Xml.newSerializer(); mSerializer.setOutput(mOutputStream, ENCODING_UTF_8); mSerializer.startDocument(ENCODING_UTF_8, true); if (!mMultiFile) { mSerializer.startTag("", TAG_SUITES); } } }
/** * Enable caching to the phone's internal storage or SD card. * * @param context the current context * @param storageDevice where to store the cached files, either {@link #DISK_CACHE_INTERNAL} or * {@link #DISK_CACHE_SDCARD}) * @return */ public boolean enableDiskCache(Context context, int storageDevice) { Context appContext = context.getApplicationContext(); String rootDir = null; if (storageDevice == DISK_CACHE_SDCARD && Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) { // SD-card available rootDir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + appContext.getPackageName(); } else { rootDir = appContext.getCacheDir().getAbsolutePath(); } this.diskCacheDirectory = rootDir + "/cachefu/" + StringSupport.underscore(name.replaceAll("\\s", "")); File outFile = new File(diskCacheDirectory); outFile.mkdirs(); isDiskCacheEnabled = outFile.exists(); if (!isDiskCacheEnabled) { Log.w(LOG_TAG, "Failed creating disk cache directory " + diskCacheDirectory); } else { Log.d(name, "enabled write through to " + diskCacheDirectory); } return isDiskCacheEnabled; }
// 图片转为文件 public static boolean saveBitmap2file(Bitmap bmp, String filepath) { CompressFormat format = CompressFormat.PNG; int quality = 100; OutputStream stream = null; try { // 判断SDcard状态 if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) { // 错误提示 return false; } // 检查SDcard空间 File SDCardRoot = Environment.getExternalStorageDirectory(); if (SDCardRoot.getFreeSpace() < 10000) { // 弹出对话框提示用户空间不够 Log.e("Utils", "存储空间不够"); return false; } // 在SDcard创建文件夹及文件 File bitmapFile = new File(SDCardRoot.getPath() + filepath); bitmapFile.getParentFile().mkdirs(); // 创建文件夹 stream = new FileOutputStream(SDCardRoot.getPath() + filepath); // "/sdcard/" } catch (FileNotFoundException e) { e.printStackTrace(); } return bmp.compress(format, quality, stream); }
public void onClick(View v) { if (v.getId() == R.id.browse) { boolean mExternalStorageAvailable = false; String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { mExternalStorageAvailable = true; } else { mExternalStorageAvailable = false; } if (mExternalStorageAvailable) { System.out.println(Environment.getExternalStorageDirectory().toString()); Log.d(TAG, Environment.getExternalStorageDirectory().toString()); mPath = new File(Environment.getExternalStorageDirectory().toString()); Log.d(TAG, mPath.toString()); mFileList = mPath.list(); onCreateDialog(AWS_APP_FILE_SELECT_DIALOG); } } else if (v.getId() == R.id.upload) { /* Switch to File List view */ Intent i = new Intent(getApplicationContext(), AwsAppGetFileListActivity.class); startActivity(i); } }
public void scanForExistingVideosAndImport() { boolean mExternalStorageAvailable = false; boolean mExternalStorageWriteable = false; File rootfolder = Environment.getExternalStorageDirectory(); String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state)) { mExternalStorageAvailable = mExternalStorageWriteable = true; } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { mExternalStorageAvailable = true; mExternalStorageWriteable = false; } else { mExternalStorageAvailable = mExternalStorageWriteable = false; } // OK, start if (mExternalStorageAvailable) { Log.d( TAG, "Starting import. OUR Directory path is : " + Environment.getExternalStorageDirectory().getAbsolutePath() + res.getString(R.string.rootSDcardFolder)); directoryScanRecurse(rootfolder); } Log.d(TAG, " Import FINISHED !"); }
/** * Method to check if external storage is available and writable. * * @return true if available and writable */ public static boolean isExternalStorageAvailable() { boolean externalStorageAvailable = false; boolean externalStorageWriteable = false; String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state)) { // We can read and write the media externalStorageAvailable = externalStorageWriteable = true; if (FrameworkContext.INFO) Log.i(TAG, "Media is available and writeable."); } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { // We can only read the media externalStorageAvailable = true; externalStorageWriteable = false; if (FrameworkContext.INFO) Log.i(TAG, "Media readonly."); } 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 = externalStorageWriteable = false; if (FrameworkContext.INFO) Log.i(TAG, "Media neither read or write."); } if (externalStorageAvailable && externalStorageWriteable) return true; else return false; }
/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) { // We can read and write the media try { url = (String) getIntent().getExtras().get(Intent.EXTRA_TEXT) + "#view:list"; } catch (Exception e) { url = "https://www.dropbox.com/s/8owsfcia59ko76i#view:list"; // Used for testing if it's not opened from the dropox action menu // Obviously what needs to happen here is a message suggesting that the user use the dropbox // app, and a textbox in case they've got the url copied. } showDialog( PROGRESS_DIALOG); // Do the work in a separate thread so we can update the UI // simultaneously. } else { // Can't write files, so show a message and quit. } }
private void takePhoto() { if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) { String dir = "sdcard/JPushDemo/pictures/"; File destDir = new File(dir); if (!destDir.exists()) { destDir.mkdirs(); } File file = new File( dir, new DateFormat().format("yyyy_MMdd_hhmmss", Calendar.getInstance(Locale.CHINA)) + ".jpg"); Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file)); setPhotoPath(file.getAbsolutePath()); try { mContext.startActivityForResult(intent, JPushDemoApplication.REQUESTCODE_TAKE_PHOTO); } catch (ActivityNotFoundException anf) { Toast.makeText( mContext, mContext.getString(R.string.camera_not_prepared), Toast.LENGTH_SHORT) .show(); } } else { Toast.makeText( mContext, mContext.getString(R.string.sdcard_not_exist_toast), Toast.LENGTH_SHORT) .show(); } }
private File file_location(String ntag) { boolean mExternalStorageAvailable = false; boolean mExternalStorageWriteable = false; String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state)) { // We can read and write the media mExternalStorageAvailable = mExternalStorageWriteable = true; } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { // We can only read the media mExternalStorageAvailable = true; mExternalStorageWriteable = false; } else { mExternalStorageAvailable = mExternalStorageWriteable = false; } if (mExternalStorageAvailable && mExternalStorageWriteable) { String ftag = dtf.format(new Date()); // return new File(Environment.getExternalStorageDirectory(), ftag+ntag); return new File(getExternalFilesDir(null), ftag + ntag); } else { mLV.addtext("No external Storage."); return null; } }
/** Checks if the application has external storage available and readable. */ public static boolean isExternalStorageReadable() { String state = Environment.getExternalStorageState(); return (state != null) && (Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)); }
/** Get SD card info detail. */ @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(); } } if (Log.isPrint) { Log.i(TAG, sd.toString()); } return sd; }
/** * 方法描述:createFile方法 * * @param String app_name * @return * @see FileUtil */ public static void createFile(String app_name) { if (android.os.Environment.MEDIA_MOUNTED.equals( android.os.Environment.getExternalStorageState())) { isCreateFileSucess = true; updateDir = new File(Environment.getExternalStorageDirectory() + "/" + KonkaApplication + "/"); updateFile = new File(updateDir + "/" + app_name + ".apk"); if (!updateDir.exists()) { updateDir.mkdirs(); } if (!updateFile.exists()) { try { updateFile.createNewFile(); } catch (IOException e) { isCreateFileSucess = false; e.printStackTrace(); } } } else { isCreateFileSucess = false; } }
public static boolean isExternalStorageWritable() { String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state)) { return true; } return false; }
private String getOutputMediaFileName() { String state = Environment.getExternalStorageState(); // Check if external storage is mounted if (!Environment.MEDIA_MOUNTED.equals(state)) { Log.e(TAG, "External storage is not mounted!"); return null; } File mediaStorageDir = new File( Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "TestingCamera2"); // Create the storage directory if it does not exist if (!mediaStorageDir.exists()) { if (!mediaStorageDir.mkdirs()) { Log.e( TAG, "Failed to create directory " + mediaStorageDir.getPath() + " for pictures/video!"); return null; } } // Create a media file name String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); String mediaFileName = mediaStorageDir.getPath() + File.separator + "VID_" + timeStamp + ".mp4"; return mediaFileName; }
/** * @return @Description : 判断额外的存储是否可用 @Creation Date : 2013-3-1 下午2:35:36 @Author : Xiaohui Chen */ public static boolean externalMemoryAvailable() { String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state)) { return true; } else { return false; } }
/* * This method checks if external storage is * writable */ private void getStorageState() { String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state)) { mExternalStorageWritable = true; } else { mExternalStorageWritable = false; } }
public static String getDiskCacheDir(Context context) { if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) || !Environment.isExternalStorageRemovable()) { return context.getExternalCacheDir().getPath(); } else { return context.getCacheDir().getPath(); } }
public boolean isSdCardMounted() { String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state)) { return true; } else { return false; } }
public boolean isExternalStorageReadable() { String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { return true; } return false; }
/** * sdcard是否可读写 * * @return */ public static boolean isSdcardReady() { try { return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()); } catch (Exception e) { LogS.e(TAG, "isSdcardReady had exception!", e); return false; } }
/** * This method checks whether SDcard is mounted or not * * @param mountPoint the mount point that should be checked * @return true if SDcard is mounted, false otherwise */ public boolean checkSDCardMount(String mountPoint) { getSDPaths(); if (mountPoint == null) { return false; } String state = null; state = mStorageManager.getVolumeState(mountPoint); return Environment.MEDIA_MOUNTED.equals(state); }
/*package*/ static boolean isExternalStorageReady() { String state = Environment.getExternalStorageState(); boolean isReady = (Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)); if (LOG) Log.v(TAG, "external storage state = " + state); if (LOG) Log.v(TAG, "external storage is ready = " + String.valueOf(isReady)); return isReady; }
@Override protected String checkPrecondition() { String t = Environment.getExternalStorageState(); if (!Environment.MEDIA_MOUNTED.equals(t)) { return "This demo requires a SD card"; } else { return null; } }
public static boolean checkSDCardMounted() { boolean canSaveExternal = false; String storageState = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(storageState)) canSaveExternal = true; else canSaveExternal = false; return canSaveExternal; }
/** * Get the external app cache directory. * * @param context The context to use * @return The external cache dir */ @TargetApi(8) public static File getExternalCacheDir(Context context) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) { if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) { return context.getExternalCacheDir(); } } return null; }