14

I'm trying to write a file to my phone.

I used Environment.getDataDirectory() to know the internal storage's path and Environment.getExternamStorageDirectory() to know the external storage's path.

But when I use Environment.getExternalStorageDirectory() as path, the file is created in internal storage. And when I use Environment.GetDataStorage() as the path, the file is not created. (I am not sure, but I can't find it in the explorer app, at least.)

I think my phone's internal storage is perceived as external storage.(In my case, it has 32 GB amount of storage)

I want to know removable storage(e.g. micro SD card) path. What should I do?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
soonoo
  • 867
  • 1
  • 10
  • 35
  • 1
    I think you need to refer to : [1]: http://stackoverflow.com/a/13648873/6127411 && [2]: http://stackoverflow.com/a/5695129/6127411 – Janki Gadhiya Apr 18 '16 at 08:23

2 Answers2

14

From the official documentation for getExternalStorageDirectory()

Don't be confused by the word "external" here. This directory can better be thought as media/shared storage. It is a filesystem that can hold a relatively large amount of data and that is shared across all applications (does not enforce permissions). Traditionally this is an SD card, but it may also be implemented as built-in storage in a device that is distinct from the protected internal storage and can be mounted as a filesystem on a computer.

So, it can be different from built-in storage in a device.

For your case, you could use getExternalStoragePublicDirectory(java.lang.String)

This is where the user will typically place and manage their own files

The path here should be one of DIRECTORY_MUSIC, DIRECTORY_PODCASTS, DIRECTORY_RINGTONES, DIRECTORY_ALARMS, DIRECTORY_NOTIFICATIONS, DIRECTORY_PICTURES, DIRECTORY_MOVIES, DIRECTORY_DOWNLOADS, or DIRECTORY_DCIM. May not be null.

Or if you want your data to be deleted whenever the user uninstalls your app, you could use getExternalFilesDir().

As these files are internal to the applications, and not typically visible to the user as media.

Also there are some differences between getFilesDir() and getExternalFilesDir()

  1. External files are not always available: they will disappear if the user mounts the external storage on a computer or removes it. See the APIs on environment for information in the storage state.

  2. There is no security enforced with these files. For example, any application holding WRITE_EXTERNAL_STORAGE can write to these files.

tony19
  • 125,647
  • 18
  • 229
  • 307
Shubhang Malviya
  • 1,525
  • 11
  • 17
  • I used `getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)` but download path just changes from `/storage/emulated/0` to `/storage/emulated/0/Download`.(still built-in storage) Do you know why? – soonoo Feb 10 '15 at 09:10
  • I think you should see the answer by commonsware http://stackoverflow.com/a/23030038/2319390 , be sure to look at his Blog post series – Shubhang Malviya Feb 10 '15 at 13:48
  • Could you please elaborate this sentence "External files are not always available: they will disappear if the user mounts the external storage on a computer or removes it"? To my understanding files will be hidden when user will mount his device to PC. But which mount mode Google is talking about? When I connect my device to PC, I am able to see all the files on PC which stored at above path. Please explain the above sentence. Thank in advance! – Vikasdeep Singh Jul 13 '15 at 10:22
-1

Try this...

static String storagestate = Environment.getExternalStorageState();



    private static FileOutputStream outStream;
    private static File imageFilepath;

    public static String saveImage(Bitmap bitmap) {
        File folder = null;

        // Check for SD card
        if (storagestate.equals(Environment.MEDIA_MOUNTED)) {
            folder = new File(Environment.getExternalStorageDirectory(),
                    "*YourStorageNameInDevice");
            if (!folder.exists()) {
                folder.mkdir();
            }
            outStream = null;

            String timestamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS")
                    .format(new Date());

            // Getting filepath
            imageFilepath = new File(folder.getPath() + File.separator
                    + timestamp + ".PNG");
            try {
                outStream = new FileOutputStream(imageFilepath);
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
                outStream.flush();
                outStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return imageFilepath.getAbsolutePath();
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131