I've reviewed numerous posts but still have no answer. After adding WRITE_EXTERNAL_STORAGE then checking that the sd card is writable and mounted, I still get "Parent directory of file is not writable." I'm using eclipse and the emulator and targeting SdkVersion="7".
How can I write to the sd card?
Example code follows:
<uses-premission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
.
.
.
checkSDStatus();
.
.
.
File sdCardRoot = Environment.getExternalStorageDirectory();
// Check state of SD card
String sdState = Environment.getExternalStorageState();
Log.d("--- state ---", sdState);
File dir = new File (sdCardRoot.getAbsolutePath() + "/download");
dir.mkdirs();
File file = new File(dir, fileName);
.
.
.
private void checkSDStatus(){
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
mExternalStorageAvailable = mExternalStorageWriteable = true;
Log.d("-- sd card --", "-- card writable --");
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
Log.d("-- sd card --", "-- card read only --");
} else {
mExternalStorageAvailable = mExternalStorageWriteable = false;
Log.d("-- sd card --", "-- card unvailable --");
}
}
Okay, I'll answer my question for others.
After adding to my manifest file several times previously I had nothing to lose so I copied another version of it and added it to the manifest below the existing permission.
The permissions appeared identical to the eye but I deleted my old one and used the new I had copied in.
I reran the app and it worked!!! Perhaps there was a bug on the disk, the manifest file wasn't getting read for some reason, or whatever - at least it now works!
If anyone knows why I'd sure like to hear the reason for this problem!
Related
Hi i am creating folder in external storage.first i need to check that Sd card is present or not.In my mobile i dont have sd card.i am using redmi mobile.Below is my code.I dnt have sd card bt i am getting sd card is mounted.Then i checked my internal storage i cant found the folder what i created.i had tried so many codes,please help me.i also added read and write external permission.
my code is:`
String state;
state=Environment.getExternalStorageState();
if(Environment.MEDIA_MOUNTED.equals(state)){
Toast.makeText(this, "Sd card found", Toast.LENGTH_SHORT).show();
File root=Environment.getExternalStorageDirectory();
File dir=new File(root.getAbsolutePath()+"/myappfile");
if(!dir.exists()){
dir.mkdir();
}
}
else {
Toast.makeText(this, "SD card not found", Toast.LENGTH_SHORT).show();
}`
Try this code -
Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
if(isSDPresent)
{
// yes SD-card is present
}
else
{
// Sorry
}
Source - https://stackoverflow.com/a/7429264/1649353
And also check Environment.isExternalStorageRemovable()
Try someting like this
if(Environment.MEDIA_MOUNTED.equals(Environnement.getExternalStorageState()))
{
File f = new File(Environment.getExternalStorageDirectory(), "/myappfile");
if (!f.exists()) {
if.mkdirs();
}
}
I need help on a tricky issue. I write an app that shall work with a file being provided on the SD card - manipulates this file - and writes it back to the SD card. So far so good. Everything is working fine within the Eclipse environment with SD emulation. As soon as I install the app on my Sony Xperia - the app does not find the file on the SD card - nor can I find files/dirs which I store/create on the SD card.
I am issuing the test-app I wrote for the Sony and kindly ask you for help on this issue!
Here is the code snippet of the app - trying to create a directory:
public class IOTestActivity extends ActionBarActivity {
private boolean mExternalStorageAvailable;
private boolean mExternalStorageWriteable;
TextView debugInfo;
TextView availInfo;
TextView writeInfo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_iotest);
debugInfo = (TextView)findViewById(R.id.iotestLbLDebugInfo);
checkSDCard();
fillView();
readSDCard();
}
private void readSDCard() {
debugInfo.append("...start reading SD dirs\n\n");
if ((mExternalStorageAvailable==true) &&
(mExternalStorageWriteable == true)) {
File root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
debugInfo.append("SD-Root='" + root + "'\n");
File newDir = new File (root, "TEST");
boolean result = newDir.mkdir();
debugInfo.append("...creating new dir '" + newDir + "' = " + result);
}
}
private void fillView() {
debugInfo.setText("...entering IO Test\n");
debugInfo.append("...reading SD-card availability\n");
availInfo = (TextView)findViewById(R.id.iotestLblInfo1);
availInfo.setText("" + mExternalStorageAvailable);
debugInfo.append("...reading SD-card writing rights\n");
writeInfo = (TextView)findViewById(R.id.iotestLblInfo2);
writeInfo.setText("" + mExternalStorageWriteable);
}
private void checkSDCard() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
// Can read and write the media
mExternalStorageAvailable = mExternalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// Can only read the media
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
} else {
// Can't read or write
mExternalStorageAvailable = mExternalStorageWriteable = false;
}
}
}
Of course I have added to my Manifest.xml:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >
Here ist the test-output of the app:
BTW: the problem exists independent from the fact if I use:
File root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
or
File root = Environment.getExternalStorageDirectory();
And when I access the SD-card via USB-connected phone through the file explorer, I always receive the same contents of the SD card:
Thanks for help!
I m trying to save a file in sd card at the following path.
Environment.getExternalStorageDirectory().getAbsolutePath() + dir + filename
if sd card state is mounted.
String state = Environment.getExternalStorageState();
One of my user complained
No backup to the extern sd card (/storage/sdcard1/)
This issue raied in Galaxy Tab2 10.1.
I am little confused,i have nexus 4,where it has only internal memory.
and verified the same in S4,s3 and note 3 all worked fine.
Hope i am saving the file in right path and works fine in all devices.
how do i resolve complain ? any things is wrong ?
.
The SD card might not be ready yet. Consider waiting for external storage to get ready, before writing to it, as shown below.
// Wait till external storage is initialized upon startup.
private void awaitExternalStorageInitialization() {
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
int count = 0;
do {
String state = Environment.getExternalStorageState();
if(count > 0) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
Logger.log(e.getMessage(), Logger.LogType.ERROR, e);
}
}
if (Environment.MEDIA_MOUNTED.equals(state)) {
// We can read and write the media
mExternalStorageAvailable = mExternalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// We can only read the media
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
} else {
// Something else is wrong. It may be one of many other states,
// but all we need to know is we can neither read nor write
mExternalStorageAvailable = mExternalStorageWriteable = false;
}
count++;
} while ((!mExternalStorageAvailable) && (!mExternalStorageWriteable)
&& (count < 15));
if(!mExternalStorageWriteable)
Logger.log("External storage not ready yet", Logger.LogType.ERROR, null);
}
Manifest has
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">
Application settings, 'Storage' = modify/delete SD card contents'
Same results on Samsung Tablet running 2.3.5 and Motorola Droid running 2.3.4.
Devices are not tethered to development machine.
Code follows:
public class OutputStudentRecords extends StActivity{
SharedPreferences mStudentSettings;
protected Cursor mCursor;
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create_csv);
String state = Environment.getExternalStorageState();
Toast.makeText(getApplicationContext(),"State is " + state, Toast.LENGTH_LONG).show();
if (!Environment.MEDIA_MOUNTED.equals(state)){
//We can read and write the media
mExternalStorageAvailable = mExternalStorageWriteable = true;
Toast.makeText(getApplicationContext(), "We Can Read And Write ", Toast.LENGTH_LONG).show();
File file = new File(Environment.getExternalStorageDirectory()
+File.separator
+"studentrecords"); //folder name
file.mkdir();
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)){
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
Toast.makeText(getApplicationContext(), "We Can Read but Not Write ", Toast.LENGTH_LONG).show();
}else{
//something else is wrong
mExternalStorageAvailable = mExternalStorageWriteable = false;
Toast.makeText(getApplicationContext(), "We Can't Read OR Write ", Toast.LENGTH_LONG).show();
}
}
}
Toast returns State="mounted" however it skips down to "we can't read or write" on both machines. I've missed something but can't find it, any help would be appreciated.
Thank you
Maybe you should change this:
if (!Environment.MEDIA_MOUNTED.equals(state)){
into this:
if (Environment.MEDIA_MOUNTED.equals(state)){
(ie: remove the "!")
In my android app I write some files to SD card.
However there is a condition that if the SD card is READ-ONLY
then I have to change screen settings accordingly.
How can I check it in my code if a SD card is read only ?
try using Environment.MEDIA_MOUNTED_READ_ONLY as:
String status = Environment.getExternalStorageState();
if (status.equalsIgnoreCase(Environment.MEDIA_MOUNTED)) {
Toast.makeText(Activity.this, "SD MEDIA_MOUNTED", Toast.LENGTH_LONG).show();
} else if (status.equalsIgnoreCase(Environment.MEDIA_MOUNTED_READ_ONLY)) {
Toast.makeText(Activity.this, "MEDIA_MOUNTED_READ_ONLY",
Toast.LENGTH_LONG).show();
}
And more full answer...
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
// We can read and write the media
mExternalStorageAvailable = mExternalStorageWriteable = true;
}
else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// We can only read the media
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
// check_folder();
} else {
// Something else is wrong. It may be one of many other states, but all we need
// to know is we can neither read nor write
mExternalStorageAvailable = mExternalStorageWriteable = false;
}