How do I get the external storage's path?

后端 未结 2 1244
青春惊慌失措
青春惊慌失措 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();
        }
    }
    

提交回复
热议问题