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

The following examples show how to use android.os.Environment#isExternalStorageRemovable() . 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
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 Project: Fairy   File: Utils.java    License: 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 3
Source Project: weex-uikit   File: WXEnvironment.java    License: 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 4
public static String getSDRootPath(){
	if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) || !Environment.isExternalStorageRemovable()) {
		return Environment.getExternalStorageDirectory().getPath();
	} else {
		return null;
	}

}
 
Example 5
Source Project: RoMote   File: ImageCache.java    License: 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 6
Source Project: graphics-samples   File: ImageCache.java    License: 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 7
Source Project: ViseFace   File: FaceDetectorActivity.java    License: 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 8
Source Project: AndroidBase   File: DiskLruCacheHelper.java    License: 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 9
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 10
Source Project: RestVolley   File: RestVolleyImageCache.java    License: 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 11
Source Project: LittleFreshWeather   File: FileUtil.java    License: 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 12
Source Project: bither-bitmap-sample   File: ImageCache.java    License: 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 13
/**
 * 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 14
/**
 * 获取硬盘的缓存目录,用来放图片
 * 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 15
Source Project: Ticket-Analysis   File: Utils.java    License: 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 16
Source Project: PicturePicker   File: Utils.java    License: 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
@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 18
/**
 * @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 19
Source Project: DaVinci   File: DiskLruImageCache.java    License: 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 20
Source Project: Dali   File: LegacySDKUtil.java    License: 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();
}