android - Directory does and doesn't exist -
i trying write file sd card on android phone, seems having issues directory. exists doesnt exist. mean when go file browser on android phone, , gpstracker folder cannot find it. yet in program seems acknowledge directory exists when checks dir.exists()
if condition below. when gets canwrite()
function returns false. have no idea wrong @ point, have debugging ideas? thanks.
boolean mexternalstorageavailable = false; boolean mexternalstoragewriteable = false; string state = environment.getexternalstoragestate(); if (environment.media_mounted.equals(state)) { // can read , write media mexternalstorageavailable = mexternalstoragewriteable = true; } else if (environment.media_mounted_read_only.equals(state)) { // can read media mexternalstorageavailable = true; mexternalstoragewriteable = false; } else { // else wrong. may 1 of many other states, need // know can neither read nor write mexternalstorageavailable = mexternalstoragewriteable = false; }
^ these both return true now, got issue fixed.
manifest:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.tinywebteam.gpstracker" android:versioncode="1" android:versionname="1.0" > <uses-permission android:name="android.permission.access_coarse_location" /> <uses-permission android:name="android.permission.internet" /> <uses-permission android:name="android.permission.read_external_storage" /> <uses-permission android:name="android.permission.write_external_storage"/> <uses-sdk android:minsdkversion="8" android:targetsdkversion="17" /> <application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name="com.tinywebteam.gpstracker.mainactivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> </application> </manifest>
code:
try { file sdcard = environment.getexternalstoragedirectory(); file dir = new file(sdcard + "/gpstracker/data"); if (!dir.exists()) { if (!dir.mkdirs()) throw new filenotfoundexception("couldn't make directory."); if (!dir.isdirectory()) throw new filenotfoundexception("couldn't verify directory."); } file fileout = new file(dir, "gpstracker_" + ts + ".csv"); if (fileout.canwrite()) { fileoutputstream out = new fileoutputstream(fileout); out.write(filestr.getbytes()); out.close(); system.out.println("file written successfully"); } else { system.out.println("could not write file"); } } catch (filenotfoundexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); }
Comments
Post a Comment