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.rsmsa.accapp.MainActivity.java

/**
 * returning image / video/*from   w w  w .j  a va2  s  .  c om*/
 */
private static File getOutputMediaFile(int type) {

    // External sdcard location
    File mediaStorageDir = new File(
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
            IMAGE_DIRECTORY_NAME);

    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create " + IMAGE_DIRECTORY_NAME + " directory");
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).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;
}

From source file:org.openmrs.mobile.activities.addeditpatient.AddEditPatientFragment.java

@NeedsPermission({ Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE })
public void capturePhoto() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getContext().getPackageManager()) != null) {
        File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
        output = new File(dir, getUniqueImageFileName());
        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(output));
        startActivityForResult(takePictureIntent, IMAGE_REQUEST);
    }/*from   w w  w  .  j a  va 2 s  . c  o m*/
}

From source file:com.apptentive.android.sdk.util.Util.java

/**
 * This function launchs the default app to view the selected file, based on mime type
 *
 * @param sourcePath/*  w ww.  j av a 2  s  .co  m*/
 * @param selectedFilePath the full path to the local storage
 * @param mimeTypeString   the mime type of the file to be opened
 * @return true if file can be viewed
 */
public static boolean openFileAttachment(final Context context, final String sourcePath,
        final String selectedFilePath, final String mimeTypeString) {
    if ((Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            || !Environment.isExternalStorageRemovable())
            && hasPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {

        File selectedFile = new File(selectedFilePath);
        String selectedFileName = null;
        if (selectedFile.exists()) {
            selectedFileName = selectedFile.getName();
            final Intent intent = new Intent();
            intent.setAction(android.content.Intent.ACTION_VIEW);
            /* Attachments were downloaded into app private data dir. In order for external app to open
             * the attachments, the file need to be copied to a download folder that is accessible to public
            * The folder will be sdcard/Downloads/apptentive-received/<file name>
            */
            File downloadFolder = Environment
                    .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
            File apptentiveSubFolder = new File(downloadFolder, "apptentive-received");
            if (!apptentiveSubFolder.exists()) {
                apptentiveSubFolder.mkdir();
            }

            File tmpfile = new File(apptentiveSubFolder, selectedFileName);
            String tmpFilePath = tmpfile.getPath();
            // If destination file already exists, overwrite it; otherwise, delete all existing files in the same folder first.
            if (!tmpfile.exists()) {
                String[] children = apptentiveSubFolder.list();
                if (children != null) {
                    for (int i = 0; i < children.length; i++) {
                        new File(apptentiveSubFolder, children[i]).delete();
                    }
                }
            }
            if (copyFile(selectedFilePath, tmpFilePath) == 0) {
                return false;
            }

            intent.setDataAndType(Uri.fromFile(tmpfile), mimeTypeString);
            try {
                context.startActivity(intent);
                return true;
            } catch (ActivityNotFoundException e) {
                ApptentiveLog.e("Activity not found to open attachment: ", e);
            }
        }
    } else {
        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(sourcePath));
        if (Util.canLaunchIntent(context, browserIntent)) {
            context.startActivity(browserIntent);
        }
    }
    return false;
}

From source file:ar.com.tristeslostrestigres.diasporanativewebapp.MainActivity.java

private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    return File.createTempFile(imageFileName, /* prefix */
            ".jpg", /* suffix */
            storageDir /* directory */
    );//from  w ww. ja va  2  s.  c om
}

From source file:com.christophergs.mbientbasic.NavigationActivity.java

public void onSaveButtonPressed(int position) {
    Log.i(TAG, String.format("SAVE TEST: %d", position));
    FragmentManager fragmentManager = getSupportFragmentManager();
    BothFragment f1 = (BothFragment) fragmentManager
            .findFragmentByTag("com.christophergs.mbientbasic.BothFragment");
    GyroFragmentNew f2 = (GyroFragmentNew) fragmentManager
            .findFragmentByTag("com.christophergs.mbientbasic.GyroFragmentNew");

    String delete_filename = String.format("METAWEAR.csv");
    File path = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
            delete_filename);//from  w  w  w .  j  ava 2  s  . c om

    //delete the csv file if it already exists (will be from older recordings)
    f1.prep(path);
    String filename = f1.saveData(false); //f1 is the accelerometer, we keep the header
    String filename2 = f2.saveData(true); //f2 is the gyro, we remove the header

    sendFile();

    /*
    if (filename != null) {
    File dataFile = getFileStreamPath(filename);
    Uri contentUri = FileProvider.getUriForFile(this, "com.mbientlab.metawear.app.fileprovider", dataFile);
            
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/plain");
    intent.putExtra(Intent.EXTRA_SUBJECT, filename);
    intent.putExtra(Intent.EXTRA_STREAM, contentUri);
    startActivity(Intent.createChooser(intent, "Saving Data"));
    }*/
}

From source file:com.example.android.camera2basic.Camera2VideoFragment.java

/** Create a File for saving an image or video */
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_DOWNLOADS), "MyCameraApp");
    // 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("MyCameraApp", "failed to create directory");
            return null;
        }/*from w ww.java  2 s . com*/
    }

    // 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 + ".jpeg");
    } else if (type == MEDIA_TYPE_VIDEO) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator + "VID_" + timeStamp + ".mp4");
    } else {
        return null;
    }

    return mediaFile;
}

From source file:de.syss.MifareClassicTool.Activities.MainMenu.java

/**
 * Open a file chooser ({@link FileChooser}). The
 * Activity result will be processed in/*from  ww w. ja  v  a2 s  .c  o m*/
 * {@link #onActivityResult(int, int, Intent)}.
 * If the dump files folder is empty display an additional error
 * message.
 * @param view The View object that triggered the method
 * (in this case the show/edit tag dump button).
 * @see FileChooser
 * @see #onActivityResult(int, int, Intent)
 */
public void onOpenTagDumpEditor(View view) {
    String dumpsDir = Environment.getExternalStoragePublicDirectory(Common.HOME_DIR) + "/" + Common.DUMPS_DIR;
    if (Common.isExternalStorageWritableErrorToast(this)) {
        File file = new File(dumpsDir);
        if (file.isDirectory() && (file.listFiles() == null || file.listFiles().length == 0)) {
            Toast.makeText(this, R.string.info_no_dumps, Toast.LENGTH_LONG).show();
        }
        Intent intent = new Intent(this, FileChooser.class);
        intent.putExtra(FileChooser.EXTRA_DIR, dumpsDir);
        intent.putExtra(FileChooser.EXTRA_TITLE, getString(R.string.text_open_dump_title));
        intent.putExtra(FileChooser.EXTRA_BUTTON_TEXT, getString(R.string.action_open_dump_file));
        intent.putExtra(FileChooser.EXTRA_ENABLE_DELETE_FILE, true);
        startActivityForResult(intent, FILE_CHOOSER_DUMP_FILE);
    }
}

From source file:de.syss.MifareClassicTool.Activities.MainMenu.java

/**
 * Open a file chooser ({@link FileChooser}). The
 * Activity result will be processed in//from  www . j  av  a  2s  .c o m
 * {@link #onActivityResult(int, int, Intent)}.
 * @param view The View object that triggered the method
 * (in this case the show/edit key button).
 * @see FileChooser
 * @see #onActivityResult(int, int, Intent)
 */
public void onOpenKeyEditor(View view) {
    if (Common.isExternalStorageWritableErrorToast(this)) {
        Intent intent = new Intent(this, FileChooser.class);
        intent.putExtra(FileChooser.EXTRA_DIR,
                Environment.getExternalStoragePublicDirectory(Common.HOME_DIR) + "/" + Common.KEYS_DIR);
        intent.putExtra(FileChooser.EXTRA_TITLE, getString(R.string.text_open_key_file_title));
        intent.putExtra(FileChooser.EXTRA_BUTTON_TEXT, getString(R.string.action_open_key_file));
        intent.putExtra(FileChooser.EXTRA_ENABLE_NEW_FILE, true);
        intent.putExtra(FileChooser.EXTRA_ENABLE_DELETE_FILE, true);
        startActivityForResult(intent, FILE_CHOOSER_KEY_FILE);
    }
}

From source file:com.att.arocollector.AROCollectorActivity.java

/**
 * Locate or Create ARO in externalStorage
 *
 * @return the path to ARO/*  ww w . j a va2 s  .c o m*/
 */
private File locateARO() {
    File path = Environment.getExternalStoragePublicDirectory("ARO");
    if (!path.exists()) {
        path.mkdir();
    }
    return path;
}

From source file:com.stfalcon.contentmanager.ContentManager.java

/**
 * Create image file in directory of pictures
 *
 * @param content//  ww  w .  j  av  a2 s. com
 * @return
 */
public static File createFile(Content content) {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String type = content.equals(Content.IMAGE) ? ".jpg" : ".mp4";
    String imageFileName = "IMAGE_" + timeStamp + "_";
    File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    File image = null;
    try {
        image = File.createTempFile(imageFileName, /* prefix */
                type, /* suffix */
                storageDir /* directory */
        );
    } catch (IOException e) {
        e.printStackTrace();
    }

    return image;
}