Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Environment.getExternalStorageDirectory().canWrite() returns false

I have checked the below code

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

The output shows sdcard mounted and writable

But the Environment.getExternalStorageDirectory().canWrite() always returns false. Can anyone advise?

like image 484
misguided Avatar asked Oct 10 '13 21:10

misguided


2 Answers

You need WRITE_EXTERNAL_STORAGE permission for your app. In addition, if you're writing a file that's being used only by your app, you should store it in Context.getExternalFilesDir(), to prevent spreading your app's files throughout public storage. If you are writing a public file, you should store it in Environment.getExternalStoragePublicDirectory().

like image 131
Joe Malin Avatar answered Nov 11 '22 05:11

Joe Malin


Check if your device has enough storage space. That could also be the problem.

like image 1
live-love Avatar answered Nov 11 '22 04:11

live-love