How do I get the external storage's path?

后端 未结 2 1237
青春惊慌失措
青春惊慌失措 2020-12-19 02:50

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

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

相关标签:
2条回答
  • 2020-12-19 02:57

    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();
        }
    }
    
    0 讨论(0)
  • 2020-12-19 03:01

    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.

    0 讨论(0)
提交回复
热议问题