Example usage for android.os Environment getExternalStoragePublicDirectory

List of usage examples for android.os Environment getExternalStoragePublicDirectory

Introduction

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

Prototype

public static File getExternalStoragePublicDirectory(String type) 

Source Link

Document

Get a top-level shared/external storage directory for placing files of a particular type.

Usage

From source file:com.commonsware.android.downloader.Downloader.java

@Override
public void onHandleIntent(Intent i) {
    try {/*w ww . ja v  a  2  s  .  co m*/
        File root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);

        root.mkdirs();

        File output = new File(root, i.getData().getLastPathSegment());

        if (output.exists()) {
            output.delete();
        }

        URL url = new URL(i.getData().toString());
        HttpURLConnection c = (HttpURLConnection) url.openConnection();

        FileOutputStream fos = new FileOutputStream(output.getPath());
        BufferedOutputStream out = new BufferedOutputStream(fos);

        try {
            InputStream in = c.getInputStream();
            byte[] buffer = new byte[8192];
            int len = 0;

            while ((len = in.read(buffer)) >= 0) {
                out.write(buffer, 0, len);
            }

            out.flush();
        } finally {
            fos.getFD().sync();
            out.close();
            c.disconnect();
        }

        LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(ACTION_COMPLETE));
    } catch (IOException e2) {
        Log.e(getClass().getName(), "Exception in download", e2);
    }
}

From source file:org.mariotaku.twidere.util.SaveFileTask.java

public static SaveFileTask saveImage(final Activity activity, final File source) {
    final String mimeType = Utils.getImageMimeType(source);
    final MimeTypeMap map = MimeTypeMap.getSingleton();
    final String extension = map.getExtensionFromMimeType(mimeType);
    if (extension == null)
        return null;
    final File pubDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    final File saveDir = new File(pubDir, "Twidere");
    return new SaveFileTask(activity, source, mimeType, saveDir);
}

From source file:Main.java

static String getDefaultMediaDirectory(int type) {
    String dirType = new String();
    switch (type) {
    case 0://from   w  w  w  .  j  ava2 s  . co m
        dirType = Environment.DIRECTORY_MUSIC;
        break;
    case 1:
        dirType = Environment.DIRECTORY_MOVIES;
        break;
    case 2:
        dirType = Environment.DIRECTORY_DCIM;
        break;
    default:
        break;
    }

    File path = new File("");
    if (type == 3) {
        // There is no API for knowing the standard location for sounds
        // such as voice recording. Though, it's typically in the 'Sounds'
        // directory at the root of the external storage
        path = new File(
                Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "Sounds");
    } else {
        path = Environment.getExternalStoragePublicDirectory(dirType);
    }

    path.mkdirs(); // make sure the directory exists

    return path.getAbsolutePath();
}

From source file:com.hplasplas.cam_capture.managers.FileSystemManager.java

private static File getDirectory(boolean needPrivate) {

    File dir;/*  w w w.  j a va2  s .c o  m*/
    if (needPrivate) {
        dir = ThisApplication.getInstance().getExternalFilesDir(PICTURE_FOLDER_NAME);
    } else {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
                && ContextCompat.checkSelfPermission(ThisApplication.getMainContext(),
                        Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {
            dir = ThisApplication.getInstance().getExternalFilesDir(PICTURE_FOLDER_NAME);
        } else {
            dir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                    PICTURE_FOLDER_NAME);
        }
    }
    if (dir != null && !dir.exists() && !dir.mkdir()) {
        throw new IllegalStateException("Dir create error");
    }
    return dir;
}

From source file:org.disrupted.rumble.util.FileUtil.java

public static File getWritableAlbumStorageDir() throws IOException {
    if (!isExternalStorageWritable())
        throw new IOException("Storage is not writable");

    File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
            RUMBLE_IMAGE_ALBUM_NAME);/*from w  w w .ja v a2 s. c o m*/
    file.mkdirs();

    if (file.getFreeSpace() < PushStatus.STATUS_ATTACHED_FILE_MAX_SIZE)
        throw new IOException("not enough space available (" + file.getFreeSpace() + "/"
                + PushStatus.STATUS_ATTACHED_FILE_MAX_SIZE + ")");

    return file;
}

From source file:com.commonsware.android.foredown.Downloader.java

@Override
public void onHandleIntent(Intent i) {
    try {//from w ww .j  av a 2s . c om
        String filename = i.getData().getLastPathSegment();

        startForeground(FOREGROUND_ID, buildForegroundNotification(filename));

        File root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);

        root.mkdirs();

        File output = new File(root, filename);

        if (output.exists()) {
            output.delete();
        }

        URL url = new URL(i.getData().toString());
        HttpURLConnection c = (HttpURLConnection) url.openConnection();
        FileOutputStream fos = new FileOutputStream(output.getPath());
        BufferedOutputStream out = new BufferedOutputStream(fos);

        try {
            InputStream in = c.getInputStream();
            byte[] buffer = new byte[8192];
            int len = 0;

            while ((len = in.read(buffer)) >= 0) {
                out.write(buffer, 0, len);
            }

            out.flush();
        } finally {
            fos.getFD().sync();
            out.close();
            c.disconnect();
        }

        stopForeground(true);
        raiseNotification(i, output, null);
    } catch (IOException e2) {
        stopForeground(true);
        raiseNotification(i, null, e2);
    }
}

From source file:com.foreignreader.WordDetailActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (BooksActivity.TESTING_STORGE)
        ObjectsFactory.storageFile = new File(getFilesDir(), "words.db");
    else {//from   ww w.  ja va 2s . c o  m
        File file = new File(
                Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getParentFile(),
                "Foreign Reader");
        file.mkdirs();
        ObjectsFactory.storageFile = new File(file, "words.db");
    }

    setContentView(R.layout.activity_word_detail);

    // Show the Up button in the action bar.
    getActionBar().setDisplayHomeAsUpEnabled(true);

    // savedInstanceState is non-null when there is fragment state
    // saved from previous configurations of this activity
    // (e.g. when rotating the screen from portrait to landscape).
    // In this case, the fragment will automatically be re-added
    // to its container so we don't need to manually add it.
    // For more information, see the Fragments API guide at:
    //
    // http://developer.android.com/guide/components/fragments.html
    //
    if (savedInstanceState == null) {
        // Create the detail fragment and add it to the activity
        // using a fragment transaction.
        Bundle arguments = new Bundle();
        arguments.putString(WordDetailFragment.ARG_ITEM_ID,
                getIntent().getStringExtra(WordDetailFragment.ARG_ITEM_ID));
        WordDetailFragment fragment = new WordDetailFragment();
        fragment.setArguments(arguments);
        getSupportFragmentManager().beginTransaction().add(R.id.word_detail_container, fragment).commit();
    }
}

From source file:com.commonsware.android.fileseditor.SampleAdapter.java

@Override
public Fragment getItem(int position) {
    File fileToEdit;//from  w ww.  j  a  va 2 s .c  o m

    switch (position) {
    case TAB_INTERNAL:
        fileToEdit = new File(ctxt.getFilesDir(), FILENAME);
        break;

    case TAB_EXTERNAL:
        fileToEdit = new File(ctxt.getExternalFilesDir(null), FILENAME);
        break;

    default:
        fileToEdit = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS),
                FILENAME);
        break;
    }

    return (EditorFragment.newInstance(fileToEdit));
}

From source file:com.lge.osclibrary.OSCCommandsExecute.java

/**
 * Return location of downloaded files from friends device in local storage
 *
 * @return local directory uri for friends camera
 *//* w  w  w .  jav  a 2  s.co m*/
public static String getFileLocation() {
    String dcimDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).toString();

    return dcimDirectory + "/friendsCameraSample";
}

From source file:com.dycody.android.idealnote.utils.StorageHelper.java

public static String getStorageDir() {
    // return Environment.getExternalStorageDirectory() + File.separator +
    // Constants.TAG + File.separator;
    return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString();
}