存储 一、SharedPreferences 二、文件存储 内部存储+SD卡存储 三、SD卡存储 四、SD卡读取 五、三级存储 1.网络获取 2.SD卡读取与写入 3.内存的获取与写入 3.主类中判断
一、SharedPreferences
1.存储数据
步骤1:得到SharedPreferences对象 getSharedPreferences(“文件的名称”,“文件的类型”);
(1).Context.MODE_PRIVATE:指定该SharedPreferences数据只能被应用程序读写
(2).Context.MODE_WORLD_READABLE:指定该SharedPreferences数据能被其他应用程序读,但不能写。
(3).Context,MODE_WORLD_WRITEABLE:指定该SharedPreferences数据能被其他应用程序写,但不能读。
步骤2:得到 SharedPreferences.Editor编辑对象
SharedPreferences.Editor editor=sp.edit();
步骤3:添加数据
editor.putBoolean(key,value)
editor.putString()
editor.putInt()
editor.putFloat()
editor.putLong()
步骤4:提交数据 editor.commit()
private void write ( ) { SharedPreferences preferences = getSharedPreferences ( "songdingxing" , MODE_PRIVATE) ; SharedPreferences. Editor editor = preferences. edit ( ) ; editor. putString ( "username" , "送定型" ) ; editor. putInt ( "age" , 18 ) ; editor. putBoolean ( "isMan" , false ) ; editor. putFloat ( "price" , 12.4f ) ; editor. putLong ( "id" , 5425054250 l) ; editor. commit ( ) ; }
2.获取数据
步骤1:得到SharedPreferences对象 getSharedPreferences(“文件的名称”,“文件的类型”);
步骤2:读取数据 String msg = sp.getString(key,defValue);
private void read ( ) { SharedPreferences preferences = getSharedPreferences ( "songdingxing" , MODE_PRIVATE) ; String username= preferences. getString ( "username" , "" ) ; int age= preferences. getInt ( "age" , 0 ) ; boolean isMan= preferences. getBoolean ( "isMan" , false ) ; float price= preferences. getFloat ( "price" , 0.0f ) ; long id= preferences. getLong ( "id" , 0 l) ; Toast. makeText ( this , username+ ":" + age+ ":" + isMan+ ":" + price+ ":" + id, Toast. LENGTH_SHORT) . show ( ) ; }
二、文件存储 内部存储+SD卡存储
三、SD卡存储
public static void write_json ( String json) { if ( Environment. getExternalStorageState ( ) . equals ( Environment. MEDIA_MOUNTED) ) { File file= Environment. getExternalStorageDirectory ( ) ; FileOutputStream fos= null; try { fos= new FileOutputStream ( new File ( file, "json.txt" ) ) ; fos. write ( json. getBytes ( ) ) ; } catch ( FileNotFoundException e) { e. printStackTrace ( ) ; } catch ( IOException e) { e. printStackTrace ( ) ; } finally { try { fos. close ( ) ; } catch ( IOException e) { e. printStackTrace ( ) ; } } } }
四、SD卡读取
public static String read_json ( ) { if ( Environment. getExternalStorageState ( ) . equals ( Environment. MEDIA_MOUNTED) ) { File file = Environment. getExternalStorageDirectory ( ) ; FileInputStream fis= null; StringBuffer sb= new StringBuffer ( ) ; try { fis= new FileInputStream ( new File ( file, "json.txt" ) ) ; byte [ ] b= new byte [ 1024 ] ; int len= 0 ; while ( ( len= fis. read ( b) ) != - 1 ) { sb. append ( new String ( b, 0 , len) ) ; } } catch ( FileNotFoundException e) { e. printStackTrace ( ) ; } catch ( IOException e) { e. printStackTrace ( ) ; } finally { try { fis. close ( ) ; } catch ( IOException e) { e. printStackTrace ( ) ; } } return sb. toString ( ) ; } else { return "" ; } }
五、三级存储
1.网络获取
public static byte [ ] getPicByte ( String url) { byte [ ] returnByte = null; try { URL url1 = new URL ( url) ; HttpURLConnection urlconn = ( HttpURLConnection) url1. openConnection ( ) ; urlconn. setRequestMethod ( "GET" ) ; urlconn. connect ( ) ; if ( urlconn. getResponseCode ( ) == 200 ) { InputStream is = urlconn. getInputStream ( ) ; ByteArrayOutputStream bos = new ByteArrayOutputStream ( ) ; int len= - 1 ; byte [ ] bytes = new byte [ 1024 ] ; while ( ( len= is. read ( bytes) ) != - 1 ) { bos. write ( bytes, 0 , len) ; } returnByte = bos. toByteArray ( ) ; } } catch ( Exception e) { e. printStackTrace ( ) ; } return returnByte; }
2.SD卡读取与写入
public static File getDatas ( ) { if ( Environment. getExternalStorageState ( ) . equals ( Environment. MEDIA_MOUNTED) ) { File path = Environment. getExternalStorageDirectory ( ) ; File file = new File ( path, "abc.jpg" ) ; if ( file. exists ( ) ) { return file; } else { return null; } } return null; }
public static void writeDatas ( byte [ ] b) { FileOutputStream fos= null; if ( Environment. getExternalStorageState ( ) . equals ( Environment. MEDIA_MOUNTED) ) { File path = Environment. getExternalStorageDirectory ( ) ; File file = new File ( path, "abc.jpg" ) ; try { file. createNewFile ( ) ; fos = new FileOutputStream ( file) ; fos. write ( b) ; } catch ( Exception e) { e. printStackTrace ( ) ; } finally { try { fos. close ( ) ; } catch ( IOException e) { e. printStackTrace ( ) ; } } } }
3.内存的获取与写入
LruCache< String, Object> lruCache; public CacheTools ( ) { int maxMemory = ( int ) Runtime. getRuntime ( ) . maxMemory ( ) ; lruCache = new LruCache < String, Object> ( maxMemory/ 8 ) { @Override protected int sizeOf ( String key, Object value) { return super . sizeOf ( key, value) ; } } ; }
public void addDatas ( String k, Object v) { if ( lruCache. get ( k) == null) { lruCache. put ( k, v) ; } }
public Object reaDatas ( String k) { return lruCache. get ( k) ; }
3.主类中判断
private void getPicture ( ) { Bitmap tutu = ( Bitmap) cacheTools. reaDatas ( "图图" ) ; Log. e ( "###lruCache" , String. valueOf ( tutu) ) ; if ( tutu!= null) { imageView. setImageBitmap ( tutu) ; Log. e ( "###111" , "通过内存" ) ; } else { File file = SDCardTools. getDatas ( ) ; if ( file!= null) { Bitmap bitmap = BitmapFactory. decodeFile ( file. getAbsolutePath ( ) ) ; imageView. setImageBitmap ( bitmap) ; cacheTools. addDatas ( "图图" , bitmap) ; Log. e ( "###111" , "通过SD" ) ; } else { try { byte [ ] b = new MyAscyncTask ( ) . execute ( "http://www.qubaobei.com/ios/cf/uploadfile/132/9/8289.jpg" ) . get ( ) ; if ( b. length> 0 ) { Bitmap bitmap = BitmapFactory. decodeByteArray ( b, 0 , b. length) ; cacheTools. addDatas ( "图图" , bitmap) ; imageView. setImageBitmap ( bitmap) ; SDCardTools. writeDatas ( b) ; Log. e ( "###111" , "通过网络" ) ; } } catch ( Exception e) { e. printStackTrace ( ) ; } } } }