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:activity.DetailsActivity.java

/**
 * Opens the document if it is on storage, else downloads it before open it.
 * //from www .j  a  va 2 s  . com
 * @param item
 *            the document to open
 */
public void openFileInMemory(final Document item) {
    mItem = item;
    MimeTypeMap map = MimeTypeMap.getSingleton();
    final String mime = map.getMimeTypeFromExtension(item.getExtension());
    mSubPath = item.getTitle() + "." + item.getExtension();

    if (mime != null) {
        if (item.isOnMemory() && Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
                || Environment.MEDIA_MOUNTED_READ_ONLY.equals(Environment.getExternalStorageState())) {
            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setDataAndType(Uri.fromFile(
                    new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
                            .getAbsolutePath() + "/" + mSubPath)),
                    mime.toLowerCase(Locale.US));

            startActivity(Intent.createChooser(i, getString(R.string.dialog_choose_app)));

        } else {
            getService().getDownloadTokenizedUrl(item.getList().getCours().getSysCode(),
                    item.getResourceString(), mDwlManagerHandler);
        }

    } else {
        DetailsActivity.this.getService().getDownloadTokenizedUrl(item.getList().getCours().getSysCode(),
                item.getResourceString(), mTokenizedURLHandler);
    }
}

From source file:com.renard.ocr.documents.creation.NewDocumentActivity.java

protected void startCamera() {
    mAnalytics.startCamera();/*from   w w w  . ja  v  a2s.  c  o  m*/
    try {
        cameraPicUri = null;
        dateCameraIntentStarted = new Date();
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";

        File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        File image;
        try {
            if (!storageDir.exists()) {
                storageDir.mkdirs();
            }
            image = new File(storageDir, imageFileName + ".jpg");
            if (image.exists()) {
                image.createNewFile();
            }
            cameraPicUri = Uri.fromFile(image);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, cameraPicUri);
            startActivityForResult(intent, REQUEST_CODE_MAKE_PHOTO);
        } catch (IOException e) {
            showFileError(PixLoadStatus.IO_ERROR);
        }

    } catch (ActivityNotFoundException e) {
        showFileError(PixLoadStatus.CAMERA_APP_NOT_FOUND);
    }
}

From source file:com.purchasingpower.inappbrowser.WebViewActivity.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);
    File imageFile = File.createTempFile(imageFileName, /* prefix */
            ".jpg", /* suffix */
            storageDir /* directory */
    );/*  w w  w .  jav a  2 s.c om*/
    return imageFile;
}

From source file:com.grokkingandroid.sampleapp.samples.data.contentprovider.lentitems.LentItemFormFragment.java

private File getDir() {
    return getAndCreateDirectory(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES));
}

From source file:com.google.android.gms.samples.vision.face.facetracker.FaceGraphic.java

public void audioPlayer(String fileName) {
    //set up MediaPlayer
    if (!setupDone) {

        try {//  ww  w .  j  av  a  2s  .  com
            File downloadDir = new File(
                    Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
                            .getAbsolutePath() + File.separator + fileName);

            File[] downloadDirs = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
                    .listFiles();

            //File exdir = Environment.getExternalStorageDirectory();
            //System.out.println("EXDIR!!!!!!!!!: " + exdir);

            for (File tmpf : downloadDirs) {
                System.out.println("FILE: " + tmpf.getAbsolutePath());
            }

            File file = new File("/storage/emulated/0/downloads" + File.separator + fileName);
            FileInputStream inputStream = new FileInputStream(downloadDir);
            mp.setDataSource(inputStream.getFD());
            mp.prepare();
            setupDone = true;

        } catch (Exception e) {
            System.out.println("ERROR READING MUSIC: " + e.getMessage());
            e.printStackTrace();
        }
    }
}

From source file:org.cm.podd.report.activity.SettingActivity.java

private File createImageFile() {
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    Log.d(TAG, "storageDir = " + storageDir + " file name = " + imageFileName);
    if (!storageDir.exists()) {
        if (!storageDir.mkdir()) {
            Log.d(TAG, "can't create directory " + storageDir);
        }//from w  w  w. ja va  2 s  .c  o  m
    }
    File image = null;
    try {
        image = File.createTempFile(imageFileName, ".jpg", storageDir);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return image;
}

From source file:de.baumann.hhsmoodle.fragmentsMain.FragmentBrowser.java

private File createImageFile() throws IOException {
    // Create an image file name
    String imageFileName = createDate();
    File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    return File.createTempFile(imageFileName, /* prefix */
            ".jpg", /* suffix */
            storageDir /* directory */
    );/*w  w  w.ja v a  2s  .c om*/
}

From source file:com.microsoft.rightsmanagement.sampleapp.App.java

@Override
public void onCreate() {
    sInstance = this;
    DECLARED_CLASS = this.getClass();
    mStorageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
}

From source file:uk.org.downiesoft.slideshow.GridViewFragment.java

/**
 * {@inheritDoc}//from   w  w w.  j a  v  a  2 s. c  o  m
 */
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    SlideShowActivity.debug(1, TAG, "onCreateView: %d %s", mCurrentImage,
            savedInstanceState == null ? "null" : savedInstanceState.toString());
    View parent = inflater.inflate(R.layout.gridview_fragment, container, false);
    mGridView = (GridView) parent.findViewById(R.id.gridview);
    mGridView.setEmptyView(parent.findViewById(R.id.emtpygridview));
    mProgressBar = (ProgressBar) parent.findViewById(R.id.gridviewProgressBar);
    mThumbSize = getThumbSizeSetting();
    mGridView.setColumnWidth(mThumbSize);
    mPreview = (PreviewFragment) getFragmentManager().findFragmentByTag(PreviewFragment.TAG);
    setHasOptionsMenu(true);
    // define actions to be taken when a thumbnail is clicked
    mGridView.setOnItemClickListener(new GridView.OnItemClickListener() {
        /**
         * {@inheritDoc}
         */
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if (mPreview == null || mGridView.getChoiceMode() == ListView.CHOICE_MODE_NONE) {
                // Preview not visible so switch to slidshow view
                mCurrentImage = position;
                mGridView.setItemChecked(position, true);
                ActivityOptions options = ActivityOptions.makeScaleUpAnimation(mGridView, (int) view.getX(),
                        (int) view.getY(), view.getWidth(), view.getHeight());
                startSlideshow(mCurrentFile, position, false, options);
            } else {
                // Preview visible so action depends on which icon was clicked
                if (mCurrentImage == position) {
                    // currently selected icon so switch to slideshow view
                    ActivityOptions options = ActivityOptions.makeScaleUpAnimation(mGridView, (int) view.getX(),
                            (int) view.getY(), view.getWidth(), view.getHeight());
                    startSlideshow(mCurrentFile, position, false, options);
                } else {
                    // different icon so select it
                    mCurrentImage = position;
                    mCurrentScrollY = mGridView.getFirstVisiblePosition();
                    mGridView.setItemChecked(position, true);
                    mPreview.setImage(mCurrentImage);
                }
            }
        }

    });
    mGridView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        /**
         * {@inheritDoc}
         */
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            // begin action mode
            mGridView.setMultiChoiceModeListener(new GridViewMultiChoiceModeListener());
            mGridView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
            mGridView.setItemChecked(position, true);
            return true;
        }
    });
    if (savedInstanceState != null) {
        SlideShowActivity.debug(1, TAG, "savedInstance: %s", savedInstanceState.toString());
        mShowingFavourites = savedInstanceState.getBoolean("showingFavourites");
        mCurrentFile = new ZFile(savedInstanceState.getString("currentFile", ""));
        if (mCurrentFile.toString().endsWith(getString(R.string.text_images_placeholder))) {
            mCurrentFile = new ZFile(mCurrentFile.getParentPath());
        }
        mCurrentImage = savedInstanceState.getInt("currentImage", 0);
        mPreviousFile = new ZFile(savedInstanceState.getString("previousFile"));
        if (mThumbnailAdapter == null) {
            mThumbnailAdapter = new ThumbnailAdapter(getActivity(), mCurrentFile, mCurrentImage, mThumbSize,
                    this);
        }
    } else {
        String defaultPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
                .toString();
        mCurrentFile = new ZFile(mSettings.getString(SettingsActivity.PREFS_LASTDIR, defaultPath));
        if (mCurrentFile.toString().endsWith(getString(R.string.text_images_placeholder))) {
            mCurrentFile = new ZFile(mCurrentFile.getParentPath());
        }
        if (!mCurrentFile.exists()) {
            mCurrentFile = new ZFile(defaultPath);
            mCurrentImage = mSettings.getInt(SettingsActivity.PREFS_CURRENTIMAGE, 0);
        }
        mPreviousFile = mCurrentFile;
        mThumbnailAdapter = new ThumbnailAdapter(getActivity(), mCurrentFile, mCurrentImage, mThumbSize, this);
    }
    mFavourites = FavouritesManager.getInstance(getActivity());
    if (savedInstanceState != null) {
        SlideShowActivity.debug(1, TAG, "onCreateView: %s", savedInstanceState.toString());
        mCurrentImage = savedInstanceState.getInt("currentImage", 0);
    }
    SlideShowActivity.debug(1, TAG, "onCreateView end: %d", mCurrentImage);
    return parent;
}

From source file:com.intervigil.micdroid.MainActivity.java

@Override
public void onExport(Recording r) {
    if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        Log.w(TAG, "onExport: External media is not available");
        Toast.makeText(mContext, R.string.recording_options_export_external_media_unavailable,
                Toast.LENGTH_SHORT).show();
        return;//from ww w  . j ava  2  s. c o  m
    }
    File externalMusicDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC);
    if (!externalMusicDir.exists() && !externalMusicDir.mkdirs()) {
        Log.e(TAG, "onExport: Failed to create external music directory");
        Toast.makeText(mContext, R.string.recording_options_export_external_music_dir_unavailable,
                Toast.LENGTH_SHORT).show();
        return;
    }

    File exported = new File(externalMusicDir, r.getName());
    FileChannel srcChannel = null;
    FileChannel dstChannel = null;
    try {
        srcChannel = mContext.openFileInput(r.getName()).getChannel();
        dstChannel = new FileOutputStream(exported).getChannel();

        srcChannel.transferTo(0, srcChannel.size(), dstChannel);

        Toast.makeText(mContext, R.string.recording_options_export_complete, Toast.LENGTH_SHORT).show();
    } catch (IOException e) {
        Log.e(TAG, "onExport: Failed to export file: " + r.getName());
        e.printStackTrace();
        Toast.makeText(mContext, R.string.recording_options_export_copy_error, Toast.LENGTH_SHORT).show();
    } finally {
        try {
            if (srcChannel != null) {
                srcChannel.close();
            }
            if (dstChannel != null) {
                dstChannel.close();
            }
        } catch (IOException e) {
            // Do nothing
        }
    }
}