1. Related code:
Add permission:
<Uses-Permission Android: Name = "android. permission. write_external_storage "/> <uses-Permission Android: Name =" android. permission. mount_unmount_filesystems "/>/** get SD card path **/Private Static string getsdpath () {string sdcardpath = NULL; Boolean sdcardexist = environment. getexternalstoragestate (). equals (Android. OS. environment. media_mounted); // determines whether the SD card exists if (sdcardexist) {sdcardpath = environment. getex Ternalstoragedirectory (); // get the root directory} If (sdcardpath! = NULL) {return sdcardpath;} else {return "";}}
Solution: change the code for obtaining the root directory:
Sdcardpath = environment. getexternalstoragedirectory (). getabsolutepath ();
In this way, you can.
Certificate ----------------------------------------------------------------------------------------------------------------------------------
Attached File Cache class:
Package com.net. utilities; import Java. io. file; import Java. io. filenotfoundexception; import Java. io. fileoutputstream; import Java. io. ioexception; import Java. io. outputstream; import Java. util. arrays; import Java. util. comparator; import android. graphics. bitmap; import android. graphics. bitmapfactory; import android. OS. environment; import android. OS. statfs; import android. util. log; public class fileoperationutil {Private Static final string tag = "filesaveutil"; Private Static final string cache_directory = "cachefile"; Private Static final int MB = 1024*1024; private Static final int max_cache_size = 10 * MB; Private Static final int least_size_of_sdcard = 10 * MB;/** get image from cache **/public static bitmap getimage (final string imageurl) {final string Path = getcachedirectory () + "/" + converturltofilename (Imageurl); // log. I (TAG, "getimage filepath:" + path); // log. I (TAG, "getimage URL:" + URL); file = new file (PATH); If (file. exists () {// log. I (TAG, "getimage file exists"); bitmap BMP = bitmapfactory. decodefile (PATH); If (BMP = NULL) {file. delete ();} else {updatefiletime (PATH); Return BMP;} return NULL;}/** Save the image to the File Cache **/public static void savebitmap (string imageurl, bitmap BM ){ If (Bm = NULL) {return;} // determine the space on the sdcard if (getfreespaceofsdcard () <least_size_of_sdcard) {// return if the SD space is insufficient ;} string filename = converturltofilename (imageurl); string dir = getcachedirectory (); file dirfile = new file (DIR); If (! Dirfile. exists () {If (! Dirfile. mkdirs () {log. W (TAG, "Create cache file directorys failed") ;}} file = new file (DIR + "/" + filename); try {file. createnewfile (); outputstream outstream = new fileoutputstream (File); BM. compress (bitmap. compressformat. JPEG, 100, outstream); outstream. flush (); outstream. close ();} catch (filenotfoundexception e) {log. W (TAG, "filenotfoundexception");} catch (ioexception e) {log. W (tag, "Ioexception") ;}}/*** calculates the file size in the storage directory, * When the total file size is greater than the specified max_cache_size or the remaining sdcard space is smaller than the specified least_size_of_sdcard *, delete the files not used recently */public static Boolean removeextracache () {file dir = new file (getcachedirectory (); file [] files = dir. listfiles (); If (Files = NULL) {return true;} If (! Android. OS. environment. getexternalstoragestate (). equals (Android. OS. environment. media_mounted) {return false;} int dirsize = 0; For (INT I = 0; I <files. length; I ++) {dirsize + = files [I]. length () ;}if (dirsize> max_cache_size | getfreespaceofsdcard () <least_size_of_sdcard) {int removenum = (INT) (0.4 * files. length) + 1);/* sort objects in ascending order based on the last modification time */arrays. sort (files, new comparator <File> () {@ Override public int compare (File file1, file file2) {If (file1.lastmodified ()> file2.lastmodified () {return 1;} else if (file1.lastmodified () = file2.lastmodified () {return 0 ;}else {return-1 ;}}); For (INT I = 0; I <removenum; I ++) {files [I]. delete ();} return true;} else {return false;}/** last modification time of the file **/public static void updatefiletime (string path) {file = new file (PATH); long newmodifiedtime = system. currenttimemillis (); file. setlastmodified (newmodifiedtime);}/** calculate the remaining space on the sdcard **/Private Static int getfreespaceofsdcard () {statfs stat = new statfs (environment. getexternalstoragedirectory (). getpath (); double sdfreesize = (double) stat. getavailableblocks () * (double) stat. getblocksize (); Return (INT) sdfreesize;} Private Static string converturltofilen Ame (string URL) {string [] STRs = URL. split ("/"); string savedimagename = STRs [STRs. length-1]; return savedimagename;}/** get cache directory **/Private Static string getcachedirectory () {string dir = getsdpath () + "/" + cache_directory; return dir;}/** get SD card path **/Private Static string getsdpath () {string sdcardpath = NULL; Boolean sdcardexist = environment. getexternalstoragestate (). equals (Android. O S. environment. media_mounted); // determine whether the SD card exists if (sdcardexist) {sdcardpath = environment. getexternalstoragedirectory (). getabsolutepath (); // obtain the root directory} If (sdcardpath! = NULL) {return sdcardpath;} else {return "";}}}
Failed to create folder on SD card, solution and File Cache