Example usage for android.os Environment getExternalStorageDirectory

List of usage examples for android.os Environment getExternalStorageDirectory

Introduction

In this page you can find the example usage for android.os Environment getExternalStorageDirectory.

Prototype

public static File getExternalStorageDirectory() 

Source Link

Document

Return the primary shared/external storage directory.

Usage

From source file:Main.java

public static String writeBitmap(byte[] data, int cameraDegree, Rect rect, Rect willTransformRect)
        throws IOException {
    File file = new File(Environment.getExternalStorageDirectory() + "/bookclip/");
    file.mkdir();/* www  . j  a  v  a2  s.c o m*/
    String bitmapPath = file.getPath() + "/" + System.currentTimeMillis() + ".png";

    // bitmap rotation, scaling, crop
    BitmapFactory.Options option = new BitmapFactory.Options();
    option.inSampleSize = 2;
    Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, option);
    Matrix bitmapMatrix = new Matrix();
    bitmapMatrix.setRotate(cameraDegree);
    int x = rect.left, y = rect.top, width = rect.right - rect.left, height = rect.bottom - rect.top;
    Bitmap rotateBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), bitmapMatrix,
            false);
    // bitmap recycle
    bitmap.recycle();

    Bitmap scaledBitmap = Bitmap.createScaledBitmap(rotateBitmap, willTransformRect.right,
            willTransformRect.bottom - willTransformRect.top, false);
    // rotatebitmap recycle
    rotateBitmap.recycle();

    Bitmap cropBitmap = Bitmap.createBitmap(scaledBitmap, x, y, width, height, null, false);
    // scaledBitmap recycle
    scaledBitmap.recycle();

    // file write
    FileOutputStream fos = new FileOutputStream(new File(bitmapPath));
    cropBitmap.compress(CompressFormat.PNG, 100, fos);
    fos.flush();
    fos.close();

    // recycle
    cropBitmap.recycle();

    return bitmapPath;
}

From source file:Main.java

@TargetApi(Build.VERSION_CODES.KITKAT)
private static String handleKitKatVersion(final Context context, final Uri uri) {
    // ExternalStorageProvider
    if (isExternalStorageDocument(uri)) {
        final String[] split = getMediaData(uri);
        final String type = split[0];

        if ("primary".equalsIgnoreCase(type)) {
            return Environment.getExternalStorageDirectory() + "/" + split[1];
        }//from  www. jav a2s  . c  om

        // TODO handle non-primary volumes
    }
    // DownloadsProvider
    else if (isDownloadsDocument(uri)) {

        final String id = DocumentsContract.getDocumentId(uri);
        final Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"),
                Long.valueOf(id));

        return getDataColumn(context, contentUri, null, null);
    }
    // MediaProvider
    else if (isMediaDocument(uri)) {
        final String[] split = getMediaData(uri);
        final String type = split[0];

        Uri contentUri = getContentUri(type);

        final String selection = "_id=?";
        final String[] selectionArgs = new String[] { split[1] };
        return getDataColumn(context, contentUri, selection, selectionArgs);
    }
    return null;
}

From source file:Main.java

public static String getRootFolder(@NonNull Context context) {
    String rootPath = null;//from w  w  w .j a v  a  2s . co m

    if (android.os.Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        rootPath = Environment.getExternalStorageDirectory().getAbsolutePath();
    } else {
        rootPath = context.getFilesDir().getAbsolutePath();
    }
    return rootPath;
}

From source file:Main.java

public static String getAbsolutePath(final Context context, final Uri uri) {
    // DocumentProvider
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && DocumentsContract.isDocumentUri(context, uri)) {
        // ExternalStorageProvider
        if (isExternalStorageDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];

            if ("primary".equalsIgnoreCase(type)) {
                return Environment.getExternalStorageDirectory() + "/" + split[1];
            }/*from   w  w w  .j  a v a2  s . co m*/

        }
        // DownloadsProvider
        else if (isDownloadsDocument(uri)) {

            final String id = DocumentsContract.getDocumentId(uri);
            final Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"),
                    Long.valueOf(id));

            return getDataColumn(context, contentUri, null, null);
        }
        // MediaProvider
        else if (isMediaDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];

            Uri contentUri = null;
            if ("image".equals(type)) {
                contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            } else if ("video".equals(type)) {
                contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
            } else if ("audio".equals(type)) {
                contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            }

            final String selection = "_id=?";
            final String[] selectionArgs = new String[] { split[1] };

            return getDataColumn(context, contentUri, selection, selectionArgs);
        }
    }
    // MediaStore (and general)
    else if ("content".equalsIgnoreCase(uri.getScheme())) {
        return getDataColumn(context, uri, null, null);
    }
    // File
    else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }

    return null;
}

From source file:Main.java

@SuppressLint("NewApi")
public static final long getUsableSpace(String accountName) {
    File savePath = Environment.getExternalStorageDirectory();
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD) {
        return savePath.getUsableSpace();

    } else {/*from   w  w  w. java  2s  . co  m*/
        StatFs stats = new StatFs(savePath.getAbsolutePath());
        return stats.getAvailableBlocks() * stats.getBlockSize();
    }

}

From source file:Main.java

private static File getDirectoryBoards() {
    File file = new File(Environment.getExternalStorageDirectory(), DIR_BOARDS_EXTERNAL);
    if (!file.exists()) {
        file.mkdir();/* ww  w .j a  va  2 s  .  c o m*/
    }

    return file;
}

From source file:Main.java

public static void cropImgFromSelfCenter(Uri uri) {
    Intent intent = new Intent("com.android.camera.action.CROP");
    intent.setDataAndType(uri, "image/*");
    intent.putExtra("crop", "true");
    intent.putExtra("aspectX", 110);
    intent.putExtra("aspectY", 135);
    intent.putExtra("outputX", 110);
    intent.putExtra("outputY", 135);
    intent.putExtra("return-data", false);
    intent.putExtra("noFaceDetection", true);
    File mFile = new File(Environment.getExternalStorageDirectory() + "/yourName");
    if (!mFile.exists())
        mFile.mkdirs();/*w w  w .j a  v  a 2 s. co  m*/
    mCropAvatar = new File(CROP_IMG_PATH);
    if (mCropAvatar.exists())
        mCropAvatar.delete();
    mCropUri = Uri.fromFile(mCropAvatar);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, mCropUri);
    mActivity.startActivityForResult(intent, REQUEST_CROP_RETURN_SELF_CENTER);
}

From source file:Main.java

public static String getRealPath(final Context context, final Uri uri) {

    // DocumentProvider
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && DocumentsContract.isDocumentUri(context, uri)) {
        // ExternalStorageProvider
        if (isExternalStorageDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];

            if ("primary".equalsIgnoreCase(type)) {
                return Environment.getExternalStorageDirectory() + "/" + split[1];
            }/*from   w  ww  .j a  v  a2s  .com*/

            // TODO handle non-primary volumes
        }
        // DownloadsProvider
        else if (isDownloadsDocument(uri)) {

            final String id = DocumentsContract.getDocumentId(uri);
            final Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"),
                    Long.valueOf(id));

            return getDataColumn(context, contentUri, null, null);
        }
        // MediaProvider
        else if (isMediaDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];

            Uri contentUri = null;
            if ("image".equals(type)) {
                contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            } else if ("video".equals(type)) {
                contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
            } else if ("audio".equals(type)) {
                contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            }

            final String selection = "_id=?";
            final String[] selectionArgs = new String[] { split[1] };

            return getDataColumn(context, contentUri, selection, selectionArgs);
        }
    }
    // MediaStore (and general)
    else if ("content".equalsIgnoreCase(uri.getScheme())) {

        // Return the remote address
        if (isGooglePhotosUri(uri))
            return uri.getLastPathSegment();

        return getDataColumn(context, uri, null, null);
    }
    // File
    else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }

    return null;
}

From source file:Main.java

/**
 * /*from   w w w. j  a va 2  s  .  c o m*/
 * @param context
 */
public static void notifyScanMediaFiles(Context context) {
    context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
            Uri.parse("file://" + Environment.getExternalStorageDirectory())));
}

From source file:Main.java

/**
 * @return the root of the filesystem containing the given path
 *//*from   w  ww .  ja va  2  s . c o  m*/
public static File getFilesystemRoot(String path) {
    File cache = Environment.getDownloadCacheDirectory();
    if (path.startsWith(cache.getPath())) {
        return cache;
    }
    File external = Environment.getExternalStorageDirectory();
    if (path.startsWith(external.getPath())) {
        return external;
    }
    throw new IllegalArgumentException("Cannot determine filesystem root for " + path);
}