Example usage for android.os Environment DIRECTORY_PICTURES

List of usage examples for android.os Environment DIRECTORY_PICTURES

Introduction

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

Prototype

String DIRECTORY_PICTURES

To view the source code for android.os Environment DIRECTORY_PICTURES.

Click Source Link

Document

Standard directory in which to place pictures that are available to the user.

Usage

From source file:Main.java

/**
 * Creates a media file in the {@code Environment.DIRECTORY_PICTURES} directory. The directory
 * is persistent and available to other applications like gallery.
 *
 * @param type Media type. Can be video or image.
 * @return A file object pointing to the newly created file.
 */// w  ww .j ava 2  s  .c om
public static File getOutputMediaFile(int type) {
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.
    if (!Environment.getExternalStorageState().equalsIgnoreCase(Environment.MEDIA_MOUNTED)) {
        return null;
    }

    File mediaStorageDir = new File(
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "CameraSample");
    // 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("CameraSample", "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 if (type == MEDIA_TYPE_SENSOR) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator + "VID_" + timeStamp + ".dat");
    } else {
        return null;
    }

    return mediaFile;
}

From source file:MainActivity.java

private Uri createFileURI() {
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(System.currentTimeMillis());
    String fileName = "PHOTO_" + timeStamp + ".jpg";
    return Uri.fromFile(
            new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), fileName));
}

From source file:Main.java

public static File getOutputMediaFile(int type, int location) {

    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.
    if (!Environment.getExternalStorageState().equalsIgnoreCase(Environment.MEDIA_MOUNTED)) {
        return null;
    }/*from ww w  .ja  v  a  2 s .c  o m*/

    File mediaStorageDir = (location == STORAGE_INT)
            ? new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                    STR_APP_NAME)
            : getActualExtSDCard(STR_APP_NAME);
    // 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(TAG, "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;
}

From source file:com.exercise.AndroidClient.RecvFrom.java

RecvFrom(DownloadManager downloadMgr, String json, String remoteHost) throws JSONException {
    this.downloadMgr = downloadMgr;
    this.remoteHost = remoteHost;
    this.json = json;
    parseJson(json);//from  w w w .j a v  a 2 s. c o m
    // setup map for getExternalStoragePublicDirectory values 
    destDirTypes = new HashMap<String, String>();
    destDirTypes.put("music", Environment.DIRECTORY_MUSIC);
    destDirTypes.put("downloads", Environment.DIRECTORY_DOWNLOADS);
    destDirTypes.put("pictures", Environment.DIRECTORY_PICTURES);
    destDirTypes.put("movies", Environment.DIRECTORY_MOVIES);
    valid = true;
}

From source file:com.google.android.apps.forscience.whistlepunk.PictureUtils.java

public static File createImageFile(long timestamp) throws IOException {
    // Create an image file name
    String imageDate = new SimpleDateFormat("yyyyMMdd-HHmmss").format(new Date(timestamp));
    String imageFileName = String.format(PICTURE_NAME_TEMPLATE, imageDate);
    File storageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
            PICTURES_DIR_NAME);//from   w w w  .  ja  va 2 s.  c  om
    if (!storageDir.exists()) {
        storageDir.mkdirs();
    }
    File imageFile = new File(storageDir, imageFileName);
    return imageFile;
}

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:com.hplasplas.cam_capture.managers.FileSystemManager.java

private static File getDirectory(boolean needPrivate) {

    File dir;//from w  w w  .  j  a va 2s . 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);/* ww w  .ja v  a 2 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.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 w ww  .  ja  va  2  s .  c  om*/
        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.krayzk9s.imgurholo.services.DownloadService.java

@Override
protected void onHandleIntent(Intent intent) {
    final NotificationManager notificationManager = (NotificationManager) this
            .getSystemService(Context.NOTIFICATION_SERVICE);
    ids = intent.getParcelableArrayListExtra("ids");
    albumName = "/";
    downloaded = 0;//  w ww  .ja  v a  2  s .  c o m
    if (ids.size() > 0) {
        albumName += intent.getStringExtra("albumName") + "/";
        File myDirectory = new File(
                android.os.Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                albumName);
        if (!myDirectory.exists()) {
            myDirectory.mkdirs();
        }
    }
    for (int i = 0; i < ids.size(); i++) {
        try {
            final String type = ids.get(i).getJSONObject().getString(ImgurHoloActivity.IMAGE_DATA_TYPE)
                    .split("/")[1];
            final String id = ids.get(i).getJSONObject().getString("id");
            final String link = ids.get(i).getJSONObject().getString(ImgurHoloActivity.IMAGE_DATA_LINK);
            Log.d("data", ids.get(i).getJSONObject().toString());
            Log.d(ImgurHoloActivity.IMAGE_DATA_TYPE,
                    ids.get(0).getJSONObject().getString(ImgurHoloActivity.IMAGE_DATA_TYPE).split("/")[1]);
            Log.d("id", ids.get(i).getJSONObject().getString("id"));
            Log.d(ImgurHoloActivity.IMAGE_DATA_LINK,
                    ids.get(0).getJSONObject().getString(ImgurHoloActivity.IMAGE_DATA_LINK));
            final NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
            notificationBuilder.setContentTitle(getString(R.string.picture_download))
                    .setContentText(getString(R.string.download_in_progress))
                    .setSmallIcon(R.drawable.icon_desaturated);
            Ion.with(getApplicationContext(), link).progress(new ProgressCallback() {
                @Override
                public void onProgress(int i, int i2) {
                    notificationBuilder.setProgress(i2, i, false);
                }
            }).write(new File(
                    android.os.Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
                            + albumName + id + "." + type))
                    .setCallback(new FutureCallback<File>() {
                        @Override
                        public void onCompleted(Exception e, File file) {
                            if (file == null)
                                return;
                            downloaded += 1;
                            if (downloaded == ids.size()) {
                                NotificationCompat.Builder notificationComplete = new NotificationCompat.Builder(
                                        getApplicationContext());
                                if (ids.size() == 1) {
                                    Intent viewImageIntent = new Intent(Intent.ACTION_VIEW);
                                    viewImageIntent.setDataAndType(Uri.fromFile(file), "image/*");
                                    Intent shareIntent = new Intent(Intent.ACTION_SEND);
                                    shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
                                    shareIntent.setType("image/*");
                                    shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
                                    PendingIntent viewImagePendingIntent = PendingIntent.getActivity(
                                            getApplicationContext(), (int) System.currentTimeMillis(),
                                            viewImageIntent, 0);
                                    PendingIntent sharePendingIntent = PendingIntent.getActivity(
                                            getApplicationContext(), (int) System.currentTimeMillis(),
                                            shareIntent, 0);
                                    notificationComplete.setContentTitle(getString(R.string.download_complete))
                                            .setSmallIcon(R.drawable.icon_desaturated)
                                            .setContentText(String.format(getString(R.string.download_progress),
                                                    downloaded))
                                            .setContentIntent(viewImagePendingIntent)
                                            .addAction(R.drawable.dark_social_share, getString(R.string.share),
                                                    sharePendingIntent);
                                } else {
                                    Intent i = new Intent(Intent.ACTION_PICK);
                                    i.setDataAndType(
                                            Uri.fromFile(new File(
                                                    android.os.Environment.getExternalStoragePublicDirectory(
                                                            Environment.DIRECTORY_PICTURES) + albumName)),
                                            "image/*");
                                    PendingIntent viewImagePendingIntent = PendingIntent.getActivity(
                                            getApplicationContext(), (int) System.currentTimeMillis(), i, 0);
                                    notificationComplete.setContentTitle(getString(R.string.download_complete))
                                            .setSmallIcon(R.drawable.icon_desaturated).setContentText(String
                                                    .format(getString(R.string.download_progress), downloaded))
                                            .setContentIntent(viewImagePendingIntent);
                                }
                                notificationManager.cancel(0);
                                notificationManager.cancel(1);
                                notificationManager.notify(1, notificationComplete.build());
                            }
                            MediaScannerConnection.scanFile(getApplicationContext(),
                                    new String[] { android.os.Environment.getExternalStoragePublicDirectory(
                                            Environment.DIRECTORY_PICTURES) + albumName + id + "." + type },
                                    null, new MediaScannerConnection.OnScanCompletedListener() {
                                        @Override
                                        public void onScanCompleted(final String path, final Uri uri) {
                                            Log.i("Scanning", String.format("Scanned path %s -> URI = %s", path,
                                                    uri.toString()));
                                        }
                                    });
                        }
                    });
            notificationManager.notify(0, notificationBuilder.build());
        } catch (JSONException e) {
            Log.e("Error!", e.toString());
        }
    }
}