1. Related code:
Join permissions:
<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); Infer if the SD card exists if (sdcardexist) { Sdcardpath = environment.getexternalstoragedirectory ();//Get root folder } if (Sdcardpath! = null) { return sdcardpath; } else { return ""; } }
Workaround: Obtain the code for the root folder instead:
Sdcardpath = Environment.getexternalstoragedirectory (). GetAbsolutePath ();
So that's it.
--------------------------------------------------------------------------------------------------------------- -------------------
Attached file Cache class:
Package Com.etnet.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;/** * picture file Save, read and write class * @author Barry */public class Fileoperationutil {private static final String TAG = "Fileoperationutil"; private static final String cache_directory = "Tqprocache"; private static final int MB = 1024*1024; private static final int max_cache_size = ten * MB; private static final int least_size_of_sdcard = ten * MB; /** get picture 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 File = new file (path); if (file.exists ()) {//LOG.I (TAG, "GetImage file exists"); Bitmap bmp = null; try {//width becomes 1/3 of the original. High also becomes 1/3 of the original. This is to reduce memory consumption, to prevent memory overflow bitmapfactory.options options=new bitmapfactory.options (); Options.insamplesize = 3; BMP = Bitmapfactory.decodefile (path,options); LOGUTIL.D (TAG, "BMP size=" +bmp.getbytecount ());} catch (Exception e) {e.printstacktrace ();} if (BMP = = null) {file.delete (); } else {updatefiletime (path); return BMP; }} return null; /** the picture into the file cache **/public static void Savebitmap (St Ring IMAGEURL, Bitmap BM) {if (BM = = null) {return; }//Infer space on SDcard if (Getfreespaceofsdcard () < Least_size_of_sdcard) {//SD space is low RetuRn } 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 File = new file (dir + "/" + filename); try {file.createnewfile (); OutputStream OutStream = new FileOutputStream (file); Bm.compress (Bitmap.CompressFormat.JPEG, OutStream); Outstream.flush (); Outstream.close (); } catch (FileNotFoundException e) {LOG.W (TAG, "FileNotFoundException"); } catch (IOException e) {LOG.W (TAG, "IOException"); }}/** * Calculates the file size under the storage folder, * When the total file size is greater than the specified Max_ca Che_size or sdcard the remaining space is less than the specified Least_size_of_sdcard * Then delete 40% files that have not been 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 (); }//LOGUTIL.D ("Barry", "dirsize=" +dirsize); if (Dirsize > Max_cache_size | | getfreespaceofsdcard () < Least_size_of_sdcard) {int removenum = (int) (( 0.4 * files.length) + 1); /* Ascending order based on the last modified time of the file */arrays.sort (Files, new comparator<file> () {@Override Publ IC 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 < files.length; i++) {LOGUTIL.D ("Barry", "File.modifiedtime=" +files[i].lastmodified ()) ;} */for (int i = 0; i < Removenum; i++) {files[i].delete (); } return true; }else{return false; }}/** change the last modified time of the file **/public static void update FileTime (String path) {File File = new file (path); Long newmodifiedtime = System.currenttimemillis (); File.setlastmodified (Newmodifiedtime); /** calculates the remaining space on the sdcard **/private static int getfrees Paceofsdcard () {StatFs stat = new StatFs (ENvironment.getexternalstoragedirectory (). GetPath ()); Double sdfreesize = (double) stat.getavailableblocks () * (double) stat.getblocksize ()); return (int) sdfreesize; } private static string Converturltofilename (string URL ) {string[] STRs = Url.split ("/"); String savedimagename = strs[strs.length-1]; return savedimagename; /** Gets the cache folder **/private static String Getcachedire Ctory () {String dir = getsdpath () + "/" + cache_directory; return dir; }/** Gets the SD card path **/private static String Getsdpath () {String sdcardpath = null; Boolean sdcardexist = Environment.getexternalstoragestate (). Equals (Android.os.Environment.MEDIA_MOUNTED); Infer if the SD card exists if (sdcardexist) {Sdcardpath = EnviROnment.getexternalstoragedirectory (). GetAbsolutePath (); Gets the root folder} if (Sdcardpath! = null) {return sdcardpath; } else {return ""; } }}
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
Android file cache and SD card creation directory failed to resolve and bitmap memory overflow resolved