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:info.guardianproject.otr.app.im.app.NewChatActivity.java

void startPhotoTaker() {

    // create Intent to take a picture and return control to the calling application
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    File photo = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM),
            "cs_" + new Date().getTime() + ".jpg");
    mLastPhoto = Uri.fromFile(photo);//from www .j av  a 2  s .  c  o  m
    intent.putExtra(MediaStore.EXTRA_OUTPUT, mLastPhoto);

    // start the image capture Intent
    startActivityForResult(intent, REQUEST_TAKE_PICTURE);
}

From source file:com.BeatYourRecord.SubmitActivity.java

private File initFile() {
    File k;//from  w  w w .  ja v  a  2s .  c  o m
    File dir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES),
            this.getClass().getPackage().getName());
    if (!dir.exists() && !dir.mkdirs()) {
        k = null;
    } else {
        k = new File(dir.getAbsolutePath(),
                new SimpleDateFormat("'BYR_tournName_dateTim_'yyyyMMddHHmmss'.mp4'").format(new Date()));
    }
    return k;
}

From source file:net.potterpcs.recipebook.RecipeData.java

public String exportRecipes(long[] ids) throws IOException {
    // TODO file selection, sharing, etc.
    String filename = "exported-recipes-" + System.currentTimeMillis() + ".rcp";
    File sd = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
    File export = new File(sd, filename);
    FileOutputStream fos = null;//from   w ww  . ja v  a 2 s.c o m
    sd.mkdirs();
    try {
        fos = new FileOutputStream(export);
        JSONArray ja = new JSONArray();
        for (long i : ids) {
            ja.put(getSingleRecipeObject(i).toJSON());
        }
        byte[] buffer = ja.toString().getBytes();
        fos.write(buffer);
        return export.toString();
    } catch (FileNotFoundException e) {
        //         Log.e(TAG, e.toString());
        return null;
    } finally {
        if (fos != null) {
            fos.close();
        }
    }
}

From source file:com.vanco.abplayer.BiliVideoViewActivity.java

@SuppressLint("SimpleDateFormat")
@Override// ww  w.  ja  v a  2 s.  co  m
public void snapshot() {
    if (!com.vanco.abplayer.view.FileUtils.sdAvailable()) {
        ToastUtils.showToast(R.string.file_explorer_sdcard_not_available);
    } else {
        Uri imgUri = null;
        Bitmap bitmap = vPlayer.getCurrentFrame();
        if (bitmap != null) {
            File screenshotsDirectory = new File(
                    Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
                            + VP.SNAP_SHOT_PATH);
            if (!screenshotsDirectory.exists()) {
                screenshotsDirectory.mkdirs();
            }

            File savePath = new File(screenshotsDirectory.getPath() + "/"
                    + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".jpg");
            if (ImageUtils.saveBitmap(savePath.getPath(), bitmap)) {
                imgUri = Uri.fromFile(savePath);
            }
        }
        if (imgUri != null) {
            sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, imgUri));
            ToastUtils.showLongToast(getString(R.string.video_screenshot_save_in, imgUri.getPath()));
        } else {
            ToastUtils.showToast(R.string.video_screenshot_failed);
        }
    }
}

From source file:net.mandaria.radioreddit.apis.RadioRedditAPI.java

public static void Download(Context context, RadioSong song, RadioEpisode episode) {
    String downloadURL = "";
    String title = "";
    String description = "radio reddit";

    String songTitle = context.getString(R.string.app_name);
    String songArtist = "";
    String filename = "";

    if (song != null && song.Download_url != null) {
        if (song.Title != null)
            songTitle = song.Title;/*from  w w  w  . j a v a  2  s  . c o m*/
        if (song.Artist != null && song.Redditor != null)
            songArtist = song.Artist + " (" + song.Redditor + ")";

        downloadURL = song.Download_url;
    } else if (episode != null && episode.Download_url != null) {
        if (episode.EpisodeTitle != null)
            songTitle = episode.EpisodeTitle;
        if (episode.ShowTitle != null)
            songArtist = episode.ShowTitle;

        downloadURL = episode.Download_url;
    }

    title = songTitle + " by " + songArtist;

    filename = songArtist + " " + songTitle + ".mp3";
    filename = filename.replace(" ", "_");

    if (!downloadURL.equals("")) {
        DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
        Uri uri = Uri.parse(downloadURL);

        File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC);
        path = new File(path, "radioreddit");
        path.mkdirs();

        DownloadManager.Request request = new DownloadManager.Request(uri);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            request.allowScanningByMediaScanner();
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        }

        request.setTitle(title).setDescription(description).setMimeType("audio/mpeg")
                .setDestinationInExternalPublicDir(Environment.DIRECTORY_MUSIC, "/radioreddit/" + filename);

        long lastDownload = -1L;
        lastDownload = downloadManager.enqueue(request);
    }
}

From source file:com.oakesville.mythling.MediaActivity.java

protected void downloadItem(final Item item) {
    try {/*  w  w  w.  ja v a 2 s.  c  om*/
        final URL baseUrl = getAppSettings().getMythTvServicesBaseUrlWithCredentials();

        String fileUrl = baseUrl + "/Content/GetFile?";
        if (item.getStorageGroup() == null)
            fileUrl += "StorageGroup=None&";
        else
            fileUrl += "StorageGroup=" + item.getStorageGroup().getName() + "&";
        fileUrl += "FileName=" + URLEncoder.encode(item.getFilePath(), "UTF-8");

        Uri uri = Uri.parse(fileUrl.toString());
        ProxyInfo proxyInfo = MediaStreamProxy.needsAuthProxy(uri);
        if (proxyInfo != null) {
            // needs proxying to support authentication since DownloadManager doesn't support
            MediaStreamProxy proxy = new MediaStreamProxy(proxyInfo,
                    AuthType.valueOf(appSettings.getMythTvServicesAuthType()));
            proxy.init();
            proxy.start();
            fileUrl = "http://" + proxy.getLocalhost().getHostAddress() + ":" + proxy.getPort() + uri.getPath();
            if (uri.getQuery() != null)
                fileUrl += "?" + uri.getQuery();
        }

        Log.i(TAG, "Media download URL: " + fileUrl);

        stopProgress();

        DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
        Request request = new Request(Uri.parse(fileUrl));
        request.setTitle(item.getOneLineTitle());
        String downloadFilePath = null;
        try {
            if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
                File downloadDir = Environment
                        .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
                if (downloadDir.exists()) {
                    String downloadPath = AppSettings.getExternalStorageDir() + "/";
                    if (getPath() != null && !getPath().isEmpty() && !getPath().equals("/"))
                        downloadPath += getPath() + "/";
                    File destDir = new File(downloadDir + "/" + downloadPath);
                    if (destDir.isDirectory() || destDir.mkdirs()) {
                        downloadFilePath = downloadPath + item.getDownloadFilename();
                        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,
                                downloadFilePath);
                        request.allowScanningByMediaScanner();
                    }
                }
            }
        } catch (IllegalStateException ex) {
            // store internal
        } catch (Exception ex) {
            // log, report and store internal
            Log.e(TAG, ex.getMessage(), ex);
            if (getAppSettings().isErrorReportingEnabled())
                new Reporter(ex).send();
            Toast.makeText(getApplicationContext(), getString(R.string.error_) + ex.toString(),
                    Toast.LENGTH_LONG).show();
        }
        long downloadId = dm.enqueue(request);
        registerDownloadReceiver(item, downloadId);
        Toast.makeText(getApplicationContext(), getString(R.string.downloading_) + item.getOneLineTitle(),
                Toast.LENGTH_LONG).show();
        getAppData().addDownload(new Download(item.getId(), downloadId, downloadFilePath, new Date()));
        if (item.isRecording() && (mediaList.isMythTv28() || getAppSettings().isMythlingMediaServices()))
            new GetCutListTask((Recording) item, downloadId).execute();
    } catch (Exception ex) {
        stopProgress();
        Log.e(TAG, ex.getMessage(), ex);
        if (getAppSettings().isErrorReportingEnabled())
            new Reporter(ex).send();
        Toast.makeText(getApplicationContext(), getString(R.string.error_) + ex.toString(), Toast.LENGTH_LONG)
                .show();
    }
}

From source file:hr.abunicic.angular.CameraActivity.java

private static File getOutputMediaFile(String ime) {
    File mediaStorageDir = new File(
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Angular");
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d(Conf.TAG, "failed to create directory");
            return null;
        }/*from  w  w w .j a va2s .c  o m*/
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());

    return new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ime + ".jpg");
}

From source file:csh.cryptonite.Cryptonite.java

private static File getDownloadDir() {
    /* Api >= 8 */
    return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
    /* Api < 8 /*from w  w  w. j  a va  2s. co m*/
    File downloadDir = new File(Environment.getExternalStorageDirectory(), "/download");
    if (!downloadDir.exists()) {
    File downloadDirD = new File(Environment.getExternalStorageDirectory(), "/Download");
    if (!downloadDirD.exists()) {
        // Make "download" dir
        downloadDir.mkdirs();
        return downloadDir;
    } else {
        return downloadDirD;
    }
    } else {
    return downloadDir;
    }*/
}

From source file:kr.wdream.storyshop.AndroidUtilities.java

private static File getAlbumDir() {
    if (Build.VERSION.SDK_INT >= 23 && ApplicationLoader.applicationContext.checkSelfPermission(
            android.Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
        return FileLoader.getInstance().getDirectory(FileLoader.MEDIA_DIR_CACHE);
    }/*www.j a va 2  s. c o m*/
    File storageDir = null;
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        storageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                "Telegram");
        if (!storageDir.mkdirs()) {
            if (!storageDir.exists()) {
                FileLog.d("tmessages", "failed to create directory");
                return null;
            }
        }
    } else {
        FileLog.d("tmessages", "External storage is not mounted READ/WRITE.");
    }

    return storageDir;
}

From source file:net.ddns.mlsoftlaberge.trycorder.TryviscamFragment.java

/**
 * Create a File for saving an image or video
 *//*from  w  w w  . j  a va2  s  .  com*/
private static File getOutputMediaFile(int type) {
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.

    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM),
            "Camera");
    // This location works best if you want the created images to be shared
    // between applications and persist after your app has been uninstalled.

    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d("Trycorder", "failed to create directory");
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
    } else if (type == MEDIA_TYPE_VIDEO) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator + "VID_" + timeStamp + ".mp4");
    } else {
        return null;
    }

    return mediaFile;
}