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

The following examples show how to use android.os.Environment#isExternalStorageRemovable() . 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 check out the related API usage on the sidebar.
Example 1
Source File: FileUtil.java    From Popeens-DSub with GNU General Public License v3.0 6 votes vote down vote up
private static File getBestDir(File[] dirs) {
	// Past 5.0 we can query directly for SD Card
	if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
		for(int i = 0; i < dirs.length; i++) {
			try {
				if (dirs[i] != null && Environment.isExternalStorageRemovable(dirs[i])) {
					return dirs[i];
				}
			} catch (Exception e) {
				Log.e(TAG, "Failed to check if is external", e);
			}
		}
	}

	// Before 5.0, we have to guess.  Most of the time the SD card is last
	for(int i = dirs.length - 1; i >= 0; i--) {
		if(dirs[i] != null) {
			return dirs[i];
		}
	}

	// Should be impossible to be reached
	return dirs[0];
}
 
Example 2
Source File: DownloadManager.java    From YalpStore with GNU General Public License v2.0 5 votes vote down vote up
private boolean isMounted(File file) {
    if (file.getAbsolutePath().startsWith(Paths.getFilesDir(context).getAbsolutePath())) {
        return true;
    }
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        return !Environment.isExternalStorageRemovable() || MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
    } else {
        return !Environment.isExternalStorageRemovable(file) || MEDIA_MOUNTED.equals(Environment.getExternalStorageState(file));
    }
}
 
Example 3
Source File: Utils.java    From Ticket-Analysis with MIT License 5 votes vote down vote up
/**
 * Check if external storage is built-in or removable.
 *
 * @return True if external storage is removable (like an SD card), false
 * otherwise.
 */
@TargetApi(9)
public static boolean isExternalStorageRemovable() {
    if (Utils.hasGingerbread()) {
        return Environment.isExternalStorageRemovable();
    }
    return true;
}
 
Example 4
Source File: FileUtils.java    From SmallGdufe-Android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 获取硬盘的缓存目录,用来放图片
 * http://blog.csdn.net/u011494050/article/details/39671159
 * @param context context
 * @return 路径
 */
private static String getDiskCacheDir(Context context) {
    String cachePath = null;
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            || !Environment.isExternalStorageRemovable()) {
        try{
            cachePath = context.getExternalCacheDir().getPath();//SDCard/Android/data/应用包名/cache/目录
        }catch (NullPointerException e){
            cachePath = context.getCacheDir().getPath();    //data/data/<application package>/cache
        }
    } else {
        cachePath = context.getCacheDir().getPath();    //data/data/<application package>/cache
    }
    return cachePath;
}
 
Example 5
Source File: ImageCache.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Check if external storage is built-in or removable.
 * 
 * @return True if external storage is removable (like an SD card), false
 *         otherwise.
 */
@TargetApi(VERSION_CODES.GINGERBREAD)
public static boolean isExternalStorageRemovable() {
	if (Utils.hasGingerbread()) {
		return Environment.isExternalStorageRemovable();
	}
	return true;
}
 
Example 6
Source File: ImageCache.java    From bither-bitmap-sample with Apache License 2.0 5 votes vote down vote up
/**
 * Check if external storage is built-in or removable.
 * 
 * @return True if external storage is removable (like an SD card), false
 *         otherwise.
 */
@TargetApi(9)
public static boolean isExternalStorageRemovable() {
	if (Utils.hasGingerbread()) {
		return Environment.isExternalStorageRemovable();
	}
	return true;
}
 
Example 7
Source File: FileUtil.java    From LittleFreshWeather with Apache License 2.0 5 votes vote down vote up
public static File getDiskCacheDir(Context context, String dirName) {
    String cacheDir;
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            || !Environment.isExternalStorageRemovable()) {
        cacheDir = context.getExternalCacheDir().getPath();
    } else {
        cacheDir = context.getCacheDir().getPath();
    }

    return new File(cacheDir + File.separator + dirName);
}
 
Example 8
Source File: RestVolleyImageCache.java    From RestVolley with Apache License 2.0 5 votes vote down vote up
/**
 * get the disk cache dir with the unique name.
 * @param context {@link Context}
 * @param uniqueName unique dir name
 * @return dir file
 */
public static File getDiskCacheDir(Context context, String uniqueName) {
    String cachePath;
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) || !Environment.isExternalStorageRemovable()) {
        cachePath = context.getExternalCacheDir().getPath();
    } else {
        cachePath = context.getCacheDir().getPath();
    }
    return new File(cachePath + File.separator + uniqueName);

}
 
Example 9
Source File: DiskLruCacheHelper.java    From AndroidBase with Apache License 2.0 5 votes vote down vote up
private File getDiskCacheDir(Context context, String uniqueName) {
    String cachePath;
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            || !Environment.isExternalStorageRemovable()) {
        cachePath = context.getExternalCacheDir().getPath();
    } else {
        cachePath = context.getCacheDir().getPath();
    }
    return new File(cachePath + File.separator + uniqueName);
}
 
Example 10
Source File: FaceDetectorActivity.java    From ViseFace with Apache License 2.0 5 votes vote down vote up
private String getDiskCacheDir(Context context, String dirName) {
    String cachePath = "";
    if ((Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            || !Environment.isExternalStorageRemovable())
            && context != null && context.getExternalCacheDir() != null) {
        cachePath = context.getExternalCacheDir().getPath();
    } else {
        if (context != null && context.getCacheDir() != null) {
            cachePath = context.getCacheDir().getPath();
        }
    }
    return cachePath + File.separator + dirName;
}
 
Example 11
Source File: ImageCache.java    From graphics-samples with Apache License 2.0 5 votes vote down vote up
/**
 * Get a usable cache directory (external if available, internal otherwise).
 *
 * @param context    The context to use
 * @param uniqueName A unique directory name to append to the cache dir
 * @return The cache dir
 */
public static File getDiskCacheDir(Context context, 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 String cachePath =
            Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) ||
                    !Environment.isExternalStorageRemovable() ?
                    context.getExternalCacheDir().getPath() :
                    context.getCacheDir().getPath();

    return new File(cachePath + File.separator + uniqueName);
}
 
Example 12
Source File: ImageCache.java    From RoMote with Apache License 2.0 5 votes vote down vote up
/**
 * Check if external storage is built-in or removable.
 *
 * @return True if external storage is removable (like an SD card), false
 *         otherwise.
 */
@TargetApi(VERSION_CODES.GINGERBREAD)
public static boolean isExternalStorageRemovable() {
    if (Utils.hasGingerbread()) {
        return Environment.isExternalStorageRemovable();
    }
    return true;
}
 
Example 13
Source File: ConUtil.java    From MegviiFacepp-Android-SDK with Apache License 2.0 5 votes vote down vote up
public static String getSDRootPath(){
	if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) || !Environment.isExternalStorageRemovable()) {
		return Environment.getExternalStorageDirectory().getPath();
	} else {
		return null;
	}

}
 
Example 14
Source File: WXEnvironment.java    From weex-uikit with MIT License 5 votes vote down vote up
public static String getDiskCacheDir(Context context) {
  if (context == null) {
    return null;
  }
  String cachePath;
  if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
          || !Environment.isExternalStorageRemovable()) {
    cachePath = context.getExternalCacheDir().getPath();
  } else {
    cachePath = context.getCacheDir().getPath();
  }
  return cachePath;
}
 
Example 15
Source File: Utils.java    From Fairy with Apache License 2.0 5 votes vote down vote up
public static File getDiskCacheDir(String uniqueName) {
    String cachePath;
    if(!"mounted".equals(Environment.getExternalStorageState()) && Environment.isExternalStorageRemovable()) {
        cachePath = App.getInstance().getCacheDir().getPath();
    } else {
        cachePath = App.getInstance().getExternalCacheDir().getPath();
    }

    return new File(cachePath + File.separator + uniqueName);
}
 
Example 16
Source File: Utils.java    From PicturePicker with Apache License 2.0 4 votes vote down vote up
private static boolean hasSDCard() {
    return Environment.getExternalStorageState().equals(
            Environment.MEDIA_MOUNTED) && !Environment.isExternalStorageRemovable();

}
 
Example 17
Source File: StorageUtils.java    From IslamicLibraryAndroid with GNU General Public License v3.0 4 votes vote down vote up
/**
 * @return A List of all storage locations available
 */
@NonNull
public static List<Storage> getAllStorageLocations(@NonNull Context context) {

/*
  This first condition is the code moving forward, since the else case is a bunch
  of unsupported hacks.

  For Kitkat and above, we rely on Environment.getExternalFilesDirs to give us a list
  of application writable directories (none of which require WRITE_EXTERNAL_STORAGE on
  Kitkat and above).

  Previously, we only would show anything if there were at least 2 entries. For M,
  some changes were made, such that on M, we even show this if there is only one
  entry.

  Irrespective of whether we require 1 entry (M) or 2 (Kitkat and L), we add an
  additional entry explicitly for the sdcard itself, (the one requiring
  WRITE_EXTERNAL_STORAGE to write).

  Thus, on Kitkat, the user may either:
  a. not see any item (if there's only one entry returned by getExternalFilesDirs, we won't
  show any options since it's the same sdcard and we have the permission and the user can't
  revoke it pre-Kitkat), or
  b. see 3+ items - /sdcard, and then at least 2 external fiels directories.

  on M, the user will always see at least 2 items (the external files dir and the actual
  external storage directory), and potentially more (depending on how many items are returned
  by getExternalFilesDirs).
 */
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        List<Storage> result = new ArrayList<>();
        int limit = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? 1 : 2;
        final File[] mountPoints = ContextCompat.getExternalFilesDirs(context, null);
        if (mountPoints != null && mountPoints.length >= limit) {
            int typeId;
            if (!Environment.isExternalStorageRemovable() || Environment.isExternalStorageEmulated()) {
                typeId = R.string.prefs_sdcard_internal;
            } else {
                typeId = R.string.prefs_sdcard_external;
            }

            int number = 1;
            result.add(new Storage(context.getString(typeId, number),
                    Environment.getExternalStorageDirectory().getAbsolutePath(),
                    Build.VERSION.SDK_INT >= Build.VERSION_CODES.M));
            for (File mountPoint : mountPoints) {
                if (mountPoint != null) {
                    result.add(new Storage(context.getString(typeId, number++),
                            mountPoint.getAbsolutePath()));
                    typeId = R.string.prefs_sdcard_external;
                }
            }
        }
        return result;
    } else {
        return getLegacyStorageLocations(context);
    }
}
 
Example 18
Source File: DiskLruImageCache.java    From DaVinci with Apache License 2.0 4 votes vote down vote up
private static boolean isExternalStorageRemovable() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
        return Environment.isExternalStorageRemovable();
    }
    return true;
}
 
Example 19
Source File: FileOperationDialogActivity.java    From Camera-Roll-Android-App with Apache License 2.0 4 votes vote down vote up
@Override
public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, @SuppressLint("RecyclerView") final int position) {
    final Album album = albums.get(position);
    ((TextView) holder.itemView.findViewById(R.id.name))
            .setText(album.getName());

    int itemCount = album.getAlbumItems().size();
    boolean oneItem = itemCount == 1;
    String count = holder.itemView.getContext().getString(oneItem ?
            R.string.item_count : R.string.items_count, itemCount);
    ((TextView) holder.itemView.findViewById(R.id.count))
            .setText(count);

    final boolean selected = position == selected_position;
    ((ViewHolder) holder).setSelected(selected);

    if (album.getAlbumItems().size() > 0) {

        AlbumItem albumItem = album.getAlbumItems().get(0);

        RequestOptions options = new RequestOptions()
                .error(R.drawable.error_placeholder)
                .signature(albumItem.getGlideSignature());

        Glide.with(holder.itemView.getContext())
                .load(albumItem.getPath())
                .apply(options)
                .into((ImageView) holder.itemView.findViewById(R.id.image));

        boolean onRemovableStorage = false;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            try {
                onRemovableStorage = Environment.isExternalStorageRemovable(new File(album.getPath()));
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            }
        }
        ImageView removableStorageIndicator = holder.itemView.findViewById(R.id.removable_storage_indicator);
        removableStorageIndicator.setVisibility(onRemovableStorage ? View.VISIBLE : View.GONE);
    }

    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            int oldSelectedPosition = selected_position;
            if (selected_position != position) {
                //un-select old item
                notifyItemChanged(oldSelectedPosition);
                selected_position = position;
            }
            //select new item
            notifyItemChanged(selected_position);
        }
    });
}
 
Example 20
Source File: LegacySDKUtil.java    From Dali with Apache License 2.0 2 votes vote down vote up
/**
 * Gets the appropriate cache dir
 *
 * @param ctx
 * @return
 */
public static String getCacheDir(Context ctx) {
    return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) || (!Environment.isExternalStorageRemovable() && ctx.getExternalCacheDir() != null) ?
            ctx.getExternalCacheDir().getPath() : ctx.getCacheDir().getPath();
}