Sd.canWrite () always returns false

I installed the following android manifest:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

My activities include the following:

 File sd = Environment.getExternalStorageDirectory(); boolean can_write = sd.canWrite(); 

sd returns / mnt / sdcard

I am testing my device (evo 3d), not an emulator. SD card installed. This happens both in charger mode and when the USB cable is not connected. The various pages on google and the messages on the stack show the same method to check if the SD card is writable, so I am missing something.

UPDATE:

I try the following code and it refuses to resolve ioexception.

  File TestFile = new File(sd + "/testfile.txt"); try { boolean result = TestFile.createNewFile(); if(result) { Log.i("tag","successfully created file"); } else { Log.i("tag","failed to create file"); } } catch (IOException e) { Log.e("tag",e.toString() + " " + TestFile); return false; } 

I formatted my SD card, mounted it again and rebooted the phone and still had the same error.

UPDATE

Apparently, it was my ignorance that made him fail. I had a permission block in the wrong section of my manifest. Thanks again:)

+6
source share
2 answers

I would not use sd.canWrite (), in fact, I would use something like this:

 String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state)) { Log.d("Test", "sdcard mounted and writable"); } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { Log.d("Test", "sdcard mounted readonly"); } else { Log.d("Test", "sdcard state: " + state); } 

Taken from here: http://www.xinotes.org/notes/note/1270/

+13
source

The canWrite () method shows false for default directories, this, in my experience, does not know the reason for this

0
source

Source: https://habr.com/ru/post/894290/


All Articles