Find location of a removable SD card

前端 未结 22 2976
南方客
南方客 2020-11-21 11:32

Is there an universal way to find the location of an external SD card?

Please, do not be confused with External Storage.

Environment.getExternalStorage

22条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-21 11:47

    I don't know why but I need to call .createNewFile() on a File created in the public storage directories before using it. In the framework the comments for that method say it isn't useful. Here's a sample...

    
     String myPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PODCASTS) + File.separator + "My Directory";
                final File myDir = new File(myPath);
                try {
                    myDir.mkdirs();
                } catch (Exception ex) {
                    Toast.makeText(this, "error: " + ex.getMessage(), Toast.LENGTH_LONG).show();
                }

            String fname = "whatever";
            File newFile = new File(myDir, fname);
    
            Log.i(TAG, "File exists --> " + newFile.exists()) //will be false  
        try {
                if (newFile.createNewFile()) {
    
                     //continue 
    
                  } else {
    
                    Log.e(TAG, "error creating file");
    
                }
    
            } catch (Exception e) {
                Log.e(TAG, e.toString());
            }
    

提交回复
热议问题