Java Code Examples for android.os.Environment#getExternalStorageState()

The following examples show how to use android.os.Environment#getExternalStorageState() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: reader   File: DirectoryManager.java    License: MIT License 8 votes vote down vote up
/**
 * 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;
}
 
Example 2
Source Project: PictureSelector   File: MediaUtils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 创建一条图片地址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];
}
 
Example 3
Source Project: HttpRequest   File: JkChatActivity.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 拍照
 * @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();
    }
}
 
Example 4
Source Project: SprintNBA   File: StorageUtils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 获取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;
}
 
Example 5
/**
 * @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;
}
 
Example 6
/**
 * 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);
}
 
Example 7
public static boolean existSDcard() {
    String externalStorageState;
    try {
        externalStorageState = Environment.getExternalStorageState();
    } catch (NullPointerException e) { // (sh)it happens (Issue #660)
        externalStorageState = "";
    }
    return MEDIA_MOUNTED.equals(externalStorageState);
}
 
Example 8
Source Project: droidddle   File: ImageManager.java    License: Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 9
Source Project: medialibrary   File: GalleryUtils.java    License: Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 10
Source Project: faceswap   File: UIUtils.java    License: Apache License 2.0 5 votes vote down vote up
private static boolean isExternalStorageWritable() {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        return true;
    }
    return false;
}
 
Example 11
Source Project: Neptune   File: PluginInstaller.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 获取插件安装目录,内置目录或者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);
}
 
Example 12
Source Project: IdealMedia   File: FileUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * @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;

}
 
Example 13
Source Project: MiBandDecompiled   File: c.java    License: Apache License 2.0 4 votes vote down vote up
public static boolean a()
{
    String s = Environment.getExternalStorageState();
    return "mounted".equals(s) || "mounted_ro".equals(s);
}
 
Example 14
@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);
    }
}
 
Example 15
Source Project: letv   File: LetvTools.java    License: Apache License 2.0 4 votes vote down vote up
public static boolean sdCardMounted() {
    String state = Environment.getExternalStorageState();
    return state.equals("mounted") && !state.equals("mounted_ro");
}
 
Example 16
protected boolean isExternalStorageReadable() {
	String state = Environment.getExternalStorageState();
	if (Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state))
		return true;
	return false;
}
 
Example 17
public static boolean isExternalStorageReadable() {
    String state = Environment.getExternalStorageState();
    return Environment.MEDIA_MOUNTED.equals(state) ||
            Environment.MEDIA_MOUNTED_READ_ONLY.equals(state);
}
 
Example 18
public boolean isExternalStorageWritable() {
    String state = Environment.getExternalStorageState();
    return Environment.MEDIA_MOUNTED.equals(state);
}
 
Example 19
Source Project: osmdroid   File: StorageUtils.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * @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);
}
 
Example 20
Source Project: Clip-Stack   File: BackupExport.java    License: MIT License 4 votes vote down vote up
private static boolean isExternalStorageWritable() {
    String state = Environment.getExternalStorageState();
    return Environment.MEDIA_MOUNTED.equals(state);
}