android.support.v4.os.EnvironmentCompat Java Examples

The following examples show how to use android.support.v4.os.EnvironmentCompat. 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: MediaStoreCompat.java    From FilePicker with MIT License 6 votes vote down vote up
private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp =
            new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
    String imageFileName = String.format("JPEG_%s.jpg", timeStamp);
    File storageDir;
    if (mCaptureStrategy.isPublic) {
        storageDir = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES);
    } else {
        storageDir = mContext.get().getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    }

    // Avoid joining path components manually
    File tempFile = new File(storageDir, imageFileName);

    // Handle the situation that user's external storage is not ready
    if (!Environment.MEDIA_MOUNTED.equals(EnvironmentCompat.getStorageState(tempFile))) {
        return null;
    }

    return tempFile;
}
 
Example #2
Source File: MediaStoreCompat.java    From AndroidDownload with Apache License 2.0 6 votes vote down vote up
private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp =
            new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
    String imageFileName = String.format("JPEG_%s.jpg", timeStamp);
    File storageDir;
    if (mCaptureStrategy.isPublic) {
        storageDir = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES);
    } else {
        storageDir = mContext.get().getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    }

    // Avoid joining path components manually
    File tempFile = new File(storageDir, imageFileName);

    // Handle the situation that user's external storage is not ready
    if (!Environment.MEDIA_MOUNTED.equals(EnvironmentCompat.getStorageState(tempFile))) {
        return null;
    }

    return tempFile;
}
 
Example #3
Source File: NetworkUtils.java    From letv with Apache License 2.0 6 votes vote down vote up
public static String type2String(int type) {
    switch (type) {
        case 0:
            return "none";
        case 1:
            return "wifi";
        case 2:
            return "2G";
        case 3:
            return "3G";
        case 4:
            return "3G";
        default:
            return EnvironmentCompat.MEDIA_UNKNOWN;
    }
}
 
Example #4
Source File: SystemUtils.java    From letv with Apache License 2.0 6 votes vote down vote up
public static String getNetworkType(Context context) {
    String networkType = EnvironmentCompat.MEDIA_UNKNOWN;
    if (context == null) {
        LOG.w(TAG, "get network type failed(null context)");
        return networkType;
    }
    ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService("connectivity");
    if (connMgr == null) {
        LOG.e(TAG, "get connectivity manager failed");
        return networkType;
    }
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
    if (networkInfo == null) {
        return networkType;
    }
    String typeName = networkInfo.getTypeName();
    String subTypeName = networkInfo.getSubtypeName();
    String extraInfo = networkInfo.getExtraInfo();
    if (TextUtils.isEmpty(subTypeName)) {
        networkType = typeName + "/" + extraInfo;
    } else {
        networkType = typeName + "(" + subTypeName + ")/" + extraInfo;
    }
    return networkType;
}
 
Example #5
Source File: MediaStoreCompat.java    From Matisse with Apache License 2.0 6 votes vote down vote up
private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp =
            new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
    String imageFileName = String.format("JPEG_%s.jpg", timeStamp);
    File storageDir;
    if (mCaptureStrategy.isPublic) {
        storageDir = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES);
    } else {
        storageDir = mContext.get().getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    }

    // Avoid joining path components manually
    File tempFile = new File(storageDir, imageFileName);

    // Handle the situation that user's external storage is not ready
    if (!Environment.MEDIA_MOUNTED.equals(EnvironmentCompat.getStorageState(tempFile))) {
        return null;
    }

    return tempFile;
}
 
Example #6
Source File: IOUtil.java    From droidddle with Apache License 2.0 6 votes vote down vote up
public static File getBestAvailableCacheRoot(Context context) {
    File[] roots = ContextCompat.getExternalCacheDirs(context);
    if (roots != null) {
        for (File root : roots) {
            if (root == null) {
                continue;
            }

            if (Environment.MEDIA_MOUNTED.equals(EnvironmentCompat.getStorageState(root))) {
                return root;
            }
        }
    }

    // Worst case, resort to internal storage
    return context.getCacheDir();
}
 
Example #7
Source File: IOUtil.java    From droidddle with Apache License 2.0 6 votes vote down vote up
public static File getBestAvailableFilesRoot(Context context) {
    File[] roots = ContextCompat.getExternalFilesDirs(context, null);
    if (roots != null) {
        for (File root : roots) {
            if (root == null) {
                continue;
            }

            if (Environment.MEDIA_MOUNTED.equals(EnvironmentCompat.getStorageState(root))) {
                return root;
            }
        }
    }

    // Worst case, resort to internal storage
    return context.getFilesDir();
}
 
Example #8
Source File: AgentWebUtils.java    From AgentWeb with Apache License 2.0 5 votes vote down vote up
static String getDiskExternalCacheDir(Context context) {
	File mFile = context.getExternalCacheDir();
	if (Environment.MEDIA_MOUNTED.equals(EnvironmentCompat.getStorageState(mFile))) {
		return mFile.getAbsolutePath();
	}
	return null;
}
 
Example #9
Source File: AgentWebX5Utils.java    From AgentWebX5 with Apache License 2.0 5 votes vote down vote up
static String getDiskExternalCacheDir(Context context) {

        File mFile = context.getExternalCacheDir();
        if (Environment.MEDIA_MOUNTED.equals(EnvironmentCompat.getStorageState(mFile)))
            return mFile.getAbsolutePath();
        return null;
    }
 
Example #10
Source File: a.java    From letv with Apache License 2.0 5 votes vote down vote up
public String a(String str) {
    if (str == null) {
        return null;
    }
    String trim = str.trim();
    if (trim.length() == 0 || "0".equals(trim) || EnvironmentCompat.MEDIA_UNKNOWN.equals(trim.toLowerCase(Locale.US))) {
        return null;
    }
    return trim;
}
 
Example #11
Source File: NetworkHelper.java    From letv with Apache License 2.0 5 votes vote down vote up
public static String generateUA(Context ctx) {
    StringBuilder buffer = new StringBuilder();
    buffer.append("Android");
    buffer.append("__");
    buffer.append("weibo");
    buffer.append("__");
    buffer.append(CommonUtils.SDK);
    buffer.append("__");
    try {
        buffer.append(ctx.getPackageManager().getPackageInfo(ctx.getPackageName(), 16).versionName.replaceAll("\\s+", EventsFilesManager.ROLL_OVER_FILE_NAME_SEPARATOR));
    } catch (Exception e) {
        buffer.append(EnvironmentCompat.MEDIA_UNKNOWN);
    }
    return buffer.toString();
}
 
Example #12
Source File: YTutils.java    From YTPlayer with GNU General Public License v3.0 4 votes vote down vote up
public static String[] getExternalStorageDirectories(Context context) {

        List<String> results = new ArrayList<>();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { //Method 1 for KitKat & above
            File[] externalDirs = context.getExternalFilesDirs(null);
            String internalRoot = Environment.getExternalStorageDirectory().getAbsolutePath().toLowerCase();

            for (File file : externalDirs) {
                if (file == null) //solved NPE on some Lollipop devices
                    continue;
                String path = file.getPath().split("/Android")[0];

                if (path.toLowerCase().startsWith(internalRoot))
                    continue;

                boolean addPath = false;

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    addPath = Environment.isExternalStorageRemovable(file);
                } else {
                    addPath = Environment.MEDIA_MOUNTED.equals(EnvironmentCompat.getStorageState(file));
                }

                if (addPath) {
                    results.add(path);
                }
            }
        }

        if (results.isEmpty()) { //Method 2 for all versions
            // better variation of: http://stackoverflow.com/a/40123073/5002496
            String output = "";
            try {
                final Process process = new ProcessBuilder().command("mount | grep /dev/block/vold")
                        .redirectErrorStream(true).start();
                process.waitFor();
                final InputStream is = process.getInputStream();
                final byte[] buffer = new byte[1024];
                while (is.read(buffer) != -1) {
                    output = output + new String(buffer);
                }
                is.close();
            } catch (final Exception e) {
                e.printStackTrace();
            }
            if (!output.trim().isEmpty()) {
                String devicePoints[] = output.split("\n");
                for (String voldPoint : devicePoints) {
                    results.add(voldPoint.split(" ")[2]);
                }
            }
        }

        //Below few lines is to remove paths which may not be external memory card, like OTG (feel free to comment them out)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            for (int i = 0; i < results.size(); i++) {
                if (!results.get(i).toLowerCase().matches(".*[0-9a-f]{4}[-][0-9a-f]{4}")) {
                    Log.d("YTUtilsTAG", results.get(i) + " might not be extSDcard");
                    results.remove(i--);
                }
            }
        } else {
            for (int i = 0; i < results.size(); i++) {
                if (!results.get(i).toLowerCase().contains("ext") && !results.get(i).toLowerCase().contains("sdcard")) {
                    Log.d("YTUtilsTAG", results.get(i) + " might not be extSDcard");
                    results.remove(i--);
                }
            }
        }

        String[] storageDirectories = new String[results.size()];
        for (int i = 0; i < results.size(); ++i) storageDirectories[i] = results.get(i);

        return storageDirectories;
    }
 
Example #13
Source File: ExternalStorageProvider.java    From FireFiles with Apache License 2.0 4 votes vote down vote up
@TargetApi(Build.VERSION_CODES.KITKAT)
private void updateVolumesLocked() {
    mRoots.clear();

    int count = 0;
    StorageUtils storageUtils = new StorageUtils(getContext());
    for (StorageVolume storageVolume : storageUtils.getStorageMounts()) {
        final File path = storageVolume.getPathFile();
        String state = EnvironmentCompat.getStorageState(path);
        final boolean mounted = Environment.MEDIA_MOUNTED.equals(state)
                || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state);
        if (!mounted) continue;

        final String rootId;
        final String title;
        if (storageVolume.isPrimary()) {
            rootId = ROOT_ID_PRIMARY_EMULATED;
            title = getContext().getString(R.string.root_internal_storage);
        } else if (storageVolume.getUuid() != null) {
            rootId = ROOT_ID_SECONDARY + storageVolume.getUuid();
            String label = storageVolume.getUserLabel();
            title = !TextUtils.isEmpty(label) ? label
                    : getContext().getString(R.string.root_external_storage)
                    + (count > 0 ? " " + count : "");
            count++;
        } else {
            Log.d(TAG, "Missing UUID for " + storageVolume.getPath() + "; skipping");
            continue;
        }

        if (mRoots.containsKey(rootId)) {
            Log.w(TAG, "Duplicate UUID " + rootId + "; skipping");
            continue;
        }

        try {
        	if(null == path.listFiles()){
        		continue;
        	}
            final RootInfo root = new RootInfo();
            mRoots.put(rootId, root);

            root.rootId = rootId;
            root.flags = Root.FLAG_SUPPORTS_CREATE | Root.FLAG_SUPPORTS_EDIT | Root.FLAG_LOCAL_ONLY | Root.FLAG_ADVANCED
                    | Root.FLAG_SUPPORTS_SEARCH | Root.FLAG_SUPPORTS_IS_CHILD;
            root.title = title;
            root.path = path;
            root.docId = getDocIdForFile(path);
        } catch (FileNotFoundException e) {
            throw new IllegalStateException(e);
        }
    }
}
 
Example #14
Source File: ExternalStorageProvider.java    From FireFiles with Apache License 2.0 4 votes vote down vote up
@TargetApi(Build.VERSION_CODES.KITKAT)
private void updateVolumesLocked() {
    mRoots.clear();

    int count = 0;
    StorageUtils storageUtils = new StorageUtils(getContext());
    for (StorageVolume storageVolume : storageUtils.getStorageMounts()) {
        final File path = storageVolume.getPathFile();
        String state = EnvironmentCompat.getStorageState(path);
        final boolean mounted = Environment.MEDIA_MOUNTED.equals(state)
                || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state);
        if (!mounted) continue;

        final String rootId;
        final String title;
        if (storageVolume.isPrimary()) {
            rootId = ROOT_ID_PRIMARY_EMULATED;
            title = getContext().getString(R.string.root_internal_storage);
        } else if (storageVolume.getUuid() != null) {
            rootId = ROOT_ID_SECONDARY + storageVolume.getUuid();
            String label = storageVolume.getUserLabel();
            title = !TextUtils.isEmpty(label) ? label
                    : getContext().getString(R.string.root_external_storage)
                    + (count > 0 ? " " + count : "");
            count++;
        } else {
            Log.d(TAG, "Missing UUID for " + storageVolume.getPath() + "; skipping");
            continue;
        }

        if (mRoots.containsKey(rootId)) {
            Log.w(TAG, "Duplicate UUID " + rootId + "; skipping");
            continue;
        }

        try {
        	if(null == path.listFiles()){
        		continue;
        	}
            final RootInfo root = new RootInfo();
            mRoots.put(rootId, root);

            root.rootId = rootId;
            root.flags = Root.FLAG_SUPPORTS_CREATE | Root.FLAG_SUPPORTS_EDIT | Root.FLAG_LOCAL_ONLY | Root.FLAG_ADVANCED
                    | Root.FLAG_SUPPORTS_SEARCH | Root.FLAG_SUPPORTS_IS_CHILD;
            root.title = title;
            root.path = path;
            root.docId = getDocIdForFile(path);
        } catch (FileNotFoundException e) {
            throw new IllegalStateException(e);
        }
    }
}
 
Example #15
Source File: ExternalStorageProvider.java    From FireFiles with Apache License 2.0 4 votes vote down vote up
@TargetApi(Build.VERSION_CODES.KITKAT)
private void updateVolumesLocked() {
    mRoots.clear();

    int count = 0;
    StorageUtils storageUtils = new StorageUtils(getContext());
    for (StorageVolume storageVolume : storageUtils.getStorageMounts()) {
        final File path = storageVolume.getPathFile();
        String state = EnvironmentCompat.getStorageState(path);
        final boolean mounted = Environment.MEDIA_MOUNTED.equals(state)
                || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state);
        if (!mounted) continue;

        final String rootId;
        final String title;
        if (storageVolume.isPrimary()) {
            rootId = ROOT_ID_PRIMARY_EMULATED;
            title = getContext().getString(R.string.root_internal_storage);
        } else if (storageVolume.getUuid() != null) {
            rootId = ROOT_ID_SECONDARY + storageVolume.getUuid();
            String label = storageVolume.getUserLabel();
            title = !TextUtils.isEmpty(label) ? label
                    : getContext().getString(R.string.root_external_storage)
                    + (count > 0 ? " " + count : "");
            count++;
        } else {
            Log.d(TAG, "Missing UUID for " + storageVolume.getPath() + "; skipping");
            continue;
        }

        if (mRoots.containsKey(rootId)) {
            Log.w(TAG, "Duplicate UUID " + rootId + "; skipping");
            continue;
        }

        try {
        	if(null == path.listFiles()){
        		continue;
        	}
            final RootInfo root = new RootInfo();
            mRoots.put(rootId, root);

            root.rootId = rootId;
            root.flags = Root.FLAG_SUPPORTS_CREATE | Root.FLAG_SUPPORTS_EDIT | Root.FLAG_LOCAL_ONLY | Root.FLAG_ADVANCED
                    | Root.FLAG_SUPPORTS_SEARCH | Root.FLAG_SUPPORTS_IS_CHILD;
            root.title = title;
            root.path = path;
            root.docId = getDocIdForFile(path);
        } catch (FileNotFoundException e) {
            throw new IllegalStateException(e);
        }
    }
}