Creating Directory in Android API 22(Lollipop) and 23(Marshmallow) -
i developing android application require files(images) saved on devices in order access them later. need create directory(folder) , put stuffs(images) in have code running on api 19(kitkat) not work on lollipop , latest marshmallow.
codes
string stored = null; file sdcard = environment.getexternalstoragedirectory() ; file folder = new file(sdcard.getabsolutefile() , "propertyimages"); log.i("folder name",folder.tostring()); if (folder.exists()){ log.w("folder exist","folder exists"); }else{ log.w("folder not exist","folder not exist"); } if (folder.mkdir()){ log.w("folder created","folder created"); }else{ log.w("folder not created","folder not created"); } file file = new file(folder.getabsolutefile(), filename + ".jpg") ; if (file.exists()) return stored ; try { fileoutputstream out = new fileoutputstream(file); bitmap.compress(bitmap.compressformat.jpeg, 90, out); out.flush(); out.close(); stored = "success"; } catch (exception e) { e.printstacktrace(); } return stored; }
in kitkat worked. in lollipop , above gives file not created
manifest
<uses-permission android:name="android.permission.write_external_storage"/>
finally got solved. @greenapps , @greybeardedgeek. @greenapp helped me lot , got me research , reading on getexternalfilesdir() solution in case needs in future
public static string createexternalstorageprivatefile(bitmap bitmap,string imagename,context ctx) { file file = new file(ctx.getexternalfilesdir("pw"), imagename + "jpg"); string stored= "stored"; try { // simple code copy picture application's // resource external file. note code // no error checking, , assumes picture small (does not // try copy in chunks). note if external storage // not mounted silently fail. fileoutputstream out = new fileoutputstream(file); bitmap.compress(bitmap.compressformat.jpeg, 90, out); out.flush(); out.close(); } catch( ioexception e ) { // unable create file, because external storage // not mounted. log.w("externalstorage", "error writing "); } return stored; }
Comments
Post a Comment