- 浏览: 498752 次
- 性别:
- 来自: 深圳
文章分类
- 全部博客 (200)
- java基础 (30)
- ajax (19)
- 乱写 (5)
- groovy (2)
- db (8)
- gwt (0)
- jee (2)
- 我关注的开源 (1)
- RIA AIR (1)
- spring (11)
- lucene (0)
- 工具 (10)
- 百科 (2)
- linux (6)
- android (40)
- 移动开发 (21)
- 代码片断 (15)
- tomcat (1)
- css (1)
- html5 (2)
- jquery (2)
- playframework (3)
- web (2)
- nio (3)
- design (1)
- nosql (3)
- 日志 (12)
- mysql (4)
- 图表 (1)
- python (3)
- ruby (1)
- git (0)
- hibernate (1)
- springboot (1)
- guava (1)
- mybatis (0)
- 工作问题 (3)
- php (1)
最新评论
-
linzm1990:
踩了很多坑啊。。。。
hibernate @Nofound 与@ManyToOne fetch lazy的问题 -
Ccccrrrrrr:
...
转: Spring boot 文件上传 -
rmzdb:
兄弟,你这个东西,在ie内核的浏览器,貌似不识别 文件名
工作问题:http下载文件,中文文件名在firefox下乱码问题 -
107x:
问题解决了,谢谢!
工作问题:http下载文件,中文文件名在firefox下乱码问题 -
klxqljq:
额鹅鹅鹅
android布局实现头尾固定, 中间多余内容可以滚动
http://www.ibm.com/developerworks/cn/xml/x-androidstorage/
存储应用程序首选项
本节介绍 Preferences API 和屏幕。Android API 提供很多方式处理首选项。其中一种方式是直接使用 SharedPreferences,并使用您自己的屏幕设计和首选项管理。第二种方法是使用 PreferenceActivity。PreferenceActivity 自动负责首选项如何呈现在屏幕上(默认情况下,看起来跟系统首选项一样),并通过使用 SharedPreferences 在用户与每个首选项交互时自动存储或保存首选项。
Preferences 屏幕的 XML 声明
<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/prefs_screen" android:key="preferencescreen" > <PreferenceCategory android:title="Assets"> <EditTextPreference android:key="@string/prefs_assetname_friendslist_key" android:title="Friends List" android:summary="Please enter filename" android:defaultValue="friends.txt" /> <EditTextPreference android:key="@string/prefs_assetname_picture_key" android:title="Picture" android:summary="Please enter filename" android:defaultValue="pict2.jpg" /> </PreferenceCategory> <PreferenceCategory android:title="Auto Settings"> <CheckBoxPreference android:key="@string/prefs_autodelete_key" android:title="Delete at Startup" android:summary="Check to clear at startup" android:defaultValue="false" /> </PreferenceCategory> </PreferenceScreen>
PreferenceScreen 包含 EditTextPreference 的两个实例、一个 CheckBoxPreference 和两个由 PreferenceCategory 定义的类别组(一个用于 Asset,另一个用于 Auto Settings)。
使用 SharedPreferences
///////////////////////////////////////////////////////////// // The following methods show how to use the SharedPreferences ///////////////////////////////////////////////////////////// /** * Retrieves the Auto delete preference * @return the value of auto delete */ public boolean prefsGetAutoDelete() { boolean v = false; SharedPreferences sprefs = PreferenceManager.getDefaultSharedPreferences(appContext); String key = appContext.getString(R.string.prefs_autodelete_key); try { v = sprefs.getBoolean(key, false); } catch (ClassCastException e) { } return v; } /** * Sets the auto delete preference * @param v the value to set */ public void prefsSetAutoDelete(boolean v) { SharedPreferences sprefs = PreferenceManager.getDefaultSharedPreferences(appContext); Editor e = sprefs.edit(); String key = appContext.getString(R.string.prefs_autodelete_key); e.putBoolean(key, v); e.commit(); }
MainActivity 插入到数据库中
String fname = prefsGetFilename(); if (fname != null && fname.length() > 0) { buffer = getAsset(fname); // Parse the JSON file String friendslist = new String(buffer); final JSONObject json = new JSONObject(friendslist); JSONArray d = json.getJSONArray("data"); int l = d.length(); for (int i2=0; i2<l; i2++) { JSONObject o = d.getJSONObject(i2); String n = o.getString("name"); String id = o.getString("id"); dbHelper.insert(id, n); } // Only the original owner thread can touch its views MainActivity.this.runOnUiThread(new Runnable() { public void run() { friendsArrayAdapter.notifyDataSetChanged(); } }); }
MainActivity Select All 和将数据绑定到 ListView
final ArrayList<Friend> dbFriends = dbHelper.listSelectAll(); if (dbFriends != null) { // Only the original owner thread can touch its views MainActivity.this.runOnUiThread(new Runnable() { public void run() { friendsArrayAdapter = new FriendsArrayAdapter( MainActivity.this, R.layout.rowlayout, dbFriends); listView.setAdapter(friendsArrayAdapter); friendsArrayAdapter.notifyDataSetChanged(); } }); }
从本地私有存储器读取数据
/** * Writes content to internal storage making the content private to * the application. The method can be easily changed to take the MODE * as argument and let the caller dictate the visibility: * MODE_PRIVATE, MODE_WORLD_WRITEABLE, MODE_WORLD_READABLE, etc. * * @param filename - the name of the file to create * @param content - the content to write */ public void writeInternalStoragePrivate( String filename, byte[] content) { try { //MODE_PRIVATE creates/replaces a file and makes // it private to your application. Other modes: // MODE_WORLD_WRITEABLE // MODE_WORLD_READABLE // MODE_APPEND FileOutputStream fos = openFileOutput(filename, Context.MODE_PRIVATE); fos.write(content); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
从内部私有存储器读取数据
/** * Reads a file from internal storage * @param filename the file to read from * @return the file content */ public byte[] readInternalStoragePrivate(String filename) { int len = 1024; byte[] buffer = new byte[len]; try { FileInputStream fis = openFileInput(filename); ByteArrayOutputStream baos = new ByteArrayOutputStream(); int nrb = fis.read(buffer, 0, len); // read up to len bytes while (nrb != -1) { baos.write(buffer, 0, nrb); nrb = fis.read(buffer, 0, len); } buffer = baos.toByteArray(); fis.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return buffer; }
从本地私有存储器删除数据
/** * Delete internal private file * @param filename - the filename to delete */ public void deleteInternalStoragePrivate(String filename) { File file = getFileStreamPath(filename); if (file != null) { file.delete(); } }
为公共数据使用设备的外部存储器
有了数据存储 API,您可以使用外部存储器存储数据。信息可以是私有的,您可以有选择地让其他应用程序对之具有读或写的访问权限。本节您将对此 API 进行编程,以便使用包括 getExternalStorageState()、getExternalFilesDir()、getExternalStorageDirectory() 和 getExternalStoragePublicDirectory() 在内的很多 API 来存储公共数据。您为公共数据使用下面的路径:/Android/data/<package_name>/files/。
在使用外部存储器之前,必须看看它是否可用,是否可写。下面两个代码片段展示了测试这些条件的帮助器方法。清单 23 测试外部存储器是否可用。
测试外部存储器是否可用
/** * Helper Method to Test if external Storage is Available */ public boolean isExternalStorageAvailable() { boolean state = false; String extStorageState = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(extStorageState)) { state = true; } return state; }
测试外部存储器是否只可读
/** * Helper Method to Test if external Storage is read only */ public boolean isExternalStorageReadOnly() { boolean state = false; String extStorageState = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(extStorageState)) { state = true; } return state; }
写到外部内存
/** * Write to external public directory * @param filename - the filename to write to * @param content - the content to write */ public void writeToExternalStoragePublic(String filename, byte[] content) { // API Level 7 or lower, use getExternalStorageDirectory() // to open a File that represents the root of the external // storage, but writing to root is not recommended, and instead // application should write to application-specific directory, as shown below. String packageName = this.getPackageName(); String path = "/Android/data/" + packageName + "/files/"; if (isExternalStorageAvailable() && !isExternalStorageReadOnly()) { try { File file = new File(path, filename); file.mkdirs(); FileOutputStream fos = new FileOutputStream(file); fos.write(content); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
从外部内存读取数据
/** * Reads a file from internal storage * @param filename - the filename to read from * @return the file contents */ public byte[] readExternallStoragePublic(String filename) { int len = 1024; byte[] buffer = new byte[len]; String packageName = this.getPackageName(); String path = "/Android/data/" + packageName + "/files/"; if (!isExternalStorageReadOnly()) { try { File file = new File(path, filename); FileInputStream fis = new FileInputStream(file); ByteArrayOutputStream baos = new ByteArrayOutputStream(); int nrb = fis.read(buffer, 0, len); //read up to len bytes while (nrb != -1) { baos.write(buffer, 0, nrb); nrb = fis.read(buffer, 0, len); } buffer = baos.toByteArray(); fis.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } return buffer; }
外部内存删除文件
/** * Delete external public file * @param filename - the filename to write to */ void deleteExternalStoragePublicFile(String filename) { String packageName = this.getPackageName(); String path = "/Android/data/" + packageName + "/files/"+filename; File file = new File(path, filename); if (file != null) { file.delete(); } }
处理外部存储器需要特殊的权限 WRITE_EXTERNAL_STORAGE,它通过 AndroidManifest.xml 请求得到
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
外部存储 API 通过根据文件类型(比如 Pictures、Ringtones)将文件存储在预先确定的目录中,允许您公共地存储文件。本文没有介绍这种方法,但是您应该熟悉它。此外,记住外部存储器中的文件任何时候都可能消失。
如果您具有不需要长期永久保存的临时文件,那么可以将这些文件存储在高速缓存中。高速缓存是一种特殊的内存,可以用于存储中小型数据(少于兆字节),但是您一定要知道,取决于有多少内存可用,高速缓存的内容任何时候都可能被清除。
检索到内部内存高速缓存的路径
/** * Helper method to retrieve the absolute path to the application * specific internal cache directory on the file system. These files * will be ones that get deleted when the application is uninstalled or when * the device runs low on storage. There is no guarantee when these * files will be deleted. * * Note: This uses a Level 8+ API. * * @return the absolute path to the application specific cache * directory */ public String getInternalCacheDirectory() { String cacheDirPath = null; File cacheDir = getCacheDir(); if (cacheDir != null) { cacheDirPath = cacheDir.getPath(); } return cacheDirPath; }
检索到外部内存高速缓存的路径
/** * Helper method to retrieve the absolute path to the application * specific external cache directory on the file system. These files * will be ones that get deleted when the application is uninstalled or when * the device runs low on storage. There is no guarantee when these * files will be deleted. * * Note: This uses a Level 8+ API. * * @return the absolute path to the application specific cache * directory */ public String getExternalCacheDirectory() { String extCacheDirPath = null; File cacheDir = getExternalCacheDir(); if (cacheDir != null) { extCacheDirPath = cacheDir.getPath(); } return extCacheDirPath; }
发表评论
-
android listview
2012-07-13 17:37 893ListView与Button的共存问题解决, 解决在list ... -
演化理解 Android 异步加载图片
2011-11-09 09:55 867LinearLayout 布局,其下放了5个ImageView ... -
android常用颜色
2011-11-07 08:49 1240常用颜色值: 可以完美的颜色比对的网站: http://w ... -
dialog,activity 屏蔽Home键详解
2011-11-03 09:39 0http://www.iteye.com/topic/1116 ... -
android SlidingDrawer example
2011-11-03 09:35 0http://disanji.net/2010/12/16/a ... -
play flash swf file in android with webview
2011-11-03 09:34 0http://androidforums.com/applic ... -
AnimationDrawable 在Dialog中不能动画的原因(转)
2011-11-03 09:33 1267原来在dialog的onCreate onStart调用的时候 ... -
Free Android UI library & component roundup
2011-11-03 09:27 1117http://java.dzone.com/articles/ ... -
Android Fundamentals: Scheduling Recurring Tasks
2011-11-03 09:26 956http://mobile.tutsplus.com/tuto ... -
Android ListView pull up to refresh 改造(转)
2011-11-03 09:25 2064转自: http://dengyin2000.iteye.co ... -
Android中dp和px之间进行转换
2011-11-03 09:02 2222在xml布局文件中,我们既可以设置px,也可以设置dp(或者d ... -
view的setTag() 和 getTag()应用
2011-10-31 12:19 29887View中的setTag(Onbect)表示给View添加一个 ... -
使用getIdentifier()获取资源Id
2011-10-31 12:15 8388使用getIdentifier()获取资源Id int i ... -
ListView的长按菜单___源码分析
2011-10-24 09:28 2566ListView的长按菜单___源码分析 Android的l ... -
让你的Android程序兼容多种分辨率
2011-10-24 09:20 995http://www.android123.com.cn/an ... -
andr菜单
2011-10-24 09:18 1174Android 菜单 菜单分为两种:系统菜单和上下文菜单。 ... -
Android 长按显示上下文菜单代码
2011-10-24 09:14 5895Android 长按显示上下文 ... -
Android Asynchronous Http Client
2011-10-19 10:27 2867转自: loopj.com/android-async-htt ... -
Android canvas.drawBitmap实现透明效果
2011-09-02 14:22 23377以下是针对,canvas.drawBitmap方法实施透明效 ... -
android资源别名
2011-08-30 14:24 2274详细请参考: http://developer.android ...
相关推荐
书中不仅结合数独游戏开发案例形象生动地讲解了Android生命周期、用户界面、2D图形、多媒体,以及本地数据存储等基础知识,而且通过“Hello, Android”项目深入探讨了外部通信、基于位置的服务、内置SQLite数据库、...
----------------------------------- Android 编程基础 1 封面----------------------------------- Android 编程基础 ...• SQLite SQLite SQLite SQLite 用作结构化的数据存储 • 多媒体支持 包括常见的音频、视频和...
第4~6章介绍2D图形、多媒体和存储本地数据;第7~10章介绍高级主题,包括联网、定位与环境感知、数据库和3D图形;附录介绍了Android与Java之间的差别以及参考书目。 本书由张波、高朝勤、杨越和徐红霞等翻译,在...
5.3.1 Intent和数据URI 5.3.2 一般操作 5.3.3 使用extra信息 5.3.4 使用组件直接调用活动 5.3.5 Intent类别 5.3.6 将Intent解析为组件的规则 5.4 练习使用ACTION_PICK 5.5 练习使用GET_...
第8章 Android的数据存储和IO 306 8.1 使用SharedPreferences 307 8.1.1 SharedPreferences与Editor 简介 307 8.1.2 SharedPreferences的存储 位置和格式 308 8.1.3 读、写其他应用Shared ...
5.3.1 Intent和数据URI 5.3.2 一般操作 5.3.3 使用extra信息 5.3.4 使用组件直接调用活动 5.3.5 Intent类别 5.3.6 将Intent解析为组件的规则 5.4 练习使用ACTION_PICK 5.5 练习使用GET_...
第8章 Android的数据存储和IO 306 8.1 使用SharedPreferences 307 8.1.1 SharedPreferences与Editor 简介 307 8.1.2 SharedPreferences的存储 位置和格式 308 8.1.3 读、写其他应用Shared Preferences 310 8.2 File...
第8章 Android的数据存储和IO 306 8.1 使用SharedPreferences 307 8.1.1 SharedPreferences与Editor 简介 307 8.1.2 SharedPreferences的存储 位置和格式 308 8.1.3 读、写其他应用Shared Preferences 310 8.2 File...
(存储选项概述) (SQLite 教程) 日历和时间计算 (differenz zweier Daten) (Java.util.date 的示例) (android 中的日历) 定时器/调度 (重复服务动作) 定时器) 图形视图 (我们的 GraphView-Element...