- 浏览: 237138 次
- 性别:
- 来自: 武汉
文章分类
- 全部博客 (195)
- android开发 (29)
- JAVA (3)
- android—Task (1)
- android—Folders (1)
- android—gallery (1)
- android—ListView (15)
- android—GridView (4)
- android—Notification (3)
- android—File (5)
- android—tabhost (3)
- android—uri (4)
- android—Shortcut (1)
- android—Imei (1)
- android—Vibrator (3)
- android—Voice (1)
- android 小案例练习 (2)
- android—Wifi (1)
- android—login (1)
- android—onKeyDown (1)
- android—Activity (12)
- android—onTouchEvent (2)
- android—thread (2)
- android—app (3)
- android—webview (2)
- android—Activity自动跳转 (2)
- android_sensor (1)
- android_URL (2)
- android—Googlemap (1)
- android TextView小练习 (1)
- android-apk (1)
- android -sqlite (2)
- Java -xml (1)
- rest (1)
- android-phone (2)
- android—image (7)
- android_intent (3)
- android——broadcastReceiver (2)
- Map (1)
- lock (0)
- android-background (2)
- android-cache (2)
- android-expandtab (2)
- android_UI控件实现 (0)
- android_viewfinderview (1)
- android-Popup (1)
- Android—TextView (0)
- Android-network (1)
- android_share (1)
- Android_pulldownview (0)
- android-Switch (1)
- android_actionbar (1)
- Android_scrollview (1)
- android_util (9)
- android-sparseArray (1)
- android_Adapter (1)
- Android—DatePicker (2)
- kjframeforandroid (1)
- DragSortListView (1)
- Afinal (1)
- Android-StaggeredGrid (1)
- SmoothProgressBar (1)
- ExplosionField (1)
- android-async-http (1)
- Android—circleindicator (1)
- android—stepsview (1)
- android—spanny (1)
- Android-ViewPager (2)
- android—pull layout (1)
- Android—time (1)
- PullToDismissPager (1)
- android—chart (1)
- android—pullzoomview (1)
- listviewfilter (1)
- andrAndroid-GIF (1)
- android—ListView,StickyScrollView (1)
- gradle (1)
- android—fragment (1)
- Android--Glide (2)
- Android - SharedPreferences (1)
- Android_imageview (2)
- dialog弹出框 (2)
- android-recyclerview (2)
- Android-Badger (1)
- android_dialog (2)
- android—RecyclerView (4)
- android TextView (1)
- android—topbar (1)
- android—轮播图效果 (1)
- Android—imageView (2)
- androidAndroid—button (1)
- 视频教程 (1)
- kotlin学习 (1)
- Android—tag (1)
- android—view (1)
- TabLayout (1)
- android-webView (1)
- rich-text (1)
- swiper标点样式 (1)
- image (1)
- ExpandableTextView (1)
- viewPager (0)
最新评论
-
龙哥IT:
把这些东西,放在一起,自己用的时候方便而已,不用到处找了
Android权限Uri.parse的几种用法 -
YURANUS_:
干货 哈哈哈
Android权限Uri.parse的几种用法 -
narutolzj:
楼主,AppUtils类是自定义的吗,找不到~~
获取安装的应用 -
black_smart:
...
Android权限Uri.parse的几种用法 -
liu_zheng:
博主 我想把文字换成图片 要怎么修改呢??
用linearLayout代替ListView
有了数据存储 API,您可以使用内部存储器存储数据。信息可以是私有的,您可以有选择地让其他应用程序对之具有读或写的访问权限。本节介绍这个存储私有数据的 API,它使用 android.content.Context.openFileInput
、openFileOutput
和 getCacheDir()
来高速缓存数据,而不是永久地存储。
清单 20 中的代码片段展示了如何从内部私有存储器读取数据。使得存储器为私有的方法是对 openFileOutput()
使用MODE_PRIVATE
。
/**
* 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();
}
}
|
清单 21 中的代码片段展示了如何从内部私有存储器读取数据;注意 openFileInput()
的使用。
/**
* 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;
}
|
清单 22 展示了如何从内部私有存储器删除数据。
/**
* 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;
}
|
清单 24 测试外部存储器是否只可读。
/**
* 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;
}
|
清单 25 展示了如何写到外部存储器,以存储公共数据。
/**
* 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();
}
}
}
|
清单 26 展示了如何从外部存储器读取数据。
/**
* 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;
}
|
清单 27 中的代码片段展示了如何从外部内存删除文件。
/**
* 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 请求得到(参见 清单 28)。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
|
外部存储 API 通过根据文件类型(比如 Pictures、Ringtones)将文件存储在预先确定的目录中,允许您公共地存储文件。本文没有介绍这种方法,但是您应该熟悉它。此外,记住外部存储器中的文件任何时候都可能消失。
如果您具有不需要长期永久保存的临时文件,那么可以将这些文件存储在高速缓存中。高速缓存是一种特殊的内存,可以用于存储中小型数据(少于兆字节),但是您一定要知道,取决于有多少内存可用,高速缓存的内容任何时候都可能被清除。
清单 29 展示了一个帮助器方法,它返回到内部内存中高速缓存的路径。
/**
* 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;
}
|
清单 30 展示了一个帮助器方法,它返回到外部内存中高速缓存的路径。
/**
* 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往文件中保存和读取数据
android 获取FileOutputStream
Context.openFileOutput(文件,权限)
Context为环境的上下文,Activity 继承了Ccontext类 比如 aa 继承了Activity 那就可以这么写 :aa.this.openFileOutput(文件,权限)
文件:既为自己创建文件的名字和文件扩展名,比如 water.txt
权限:android有四种权限
1.Context.MODE_PRIVATE
私有属性,只有自己可以访问,并且第二次写入的内容会覆盖第一次写入的内容
2.Context.MODE_APPEND
私有属性,只有自己可以访问,第二次写入的内容会追加到第一次写入的内容的后面
3.Context.MODE_WORLD_WRITEABLE
公有属性,其它项目都可以写入,不过第二次写入的内容会覆盖第一次写入的内容
4.Context.MODE_WORLD_READABLE
公有属性,其它项目都可以读取
权限是可以相加的,比如
现在我想要一个其它项目可以读取,并且也可以写入,还可以追加
Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE+Context.MODE_APPEND
android 创建的文件会放到手机上的/data/data/包名/files下
android 获取FileInputStream
Context.openInFileOutput(文件)
Context为环境的上下文,Activity 继承了Context类
相关推荐
android 写了一个工具类实现txt文件保存和读取
Android----文件的保存与读取 设置简单的界面,有提交按钮, 在文本编辑框输入内容,单击按钮 将内容保存!
Android中内部文件保存读取,sd卡文件保存读取,首选项的保存和读取还有数据库的数据保存和读取。非常全
android下读取framebuffer的内容 程序运行就会把framebuffer的内容保存在/sdcard/123.raw下面, 要想打开还需要在文件上插入如bitmap的文件头即可
结构思路:通过创建FileService类来实现save和read方法,MainActivity的控件调用方法实现对文件数据保存与读取。
android 使用SharedPreferences对数据存储/删除/读取/文件删除
SharedPreferences 保存读取数据, 使用AutoCompleteTextView 和SharedPreferences 实现历史记录提示。调用文件管理器自定义路径读取文件
主要介绍了Android采用File形式保存与读取数据的方法,涉及Android文件流操作的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
前言 相信大家都知道知道,在AndroidOS中,提供了五中数据存储方式,分别是:ContentProvider存储、文件存储、SharedPreference...我们在学Java的时候都知道,Java中的IO操作来进行文件的保存和读取,Android是基于Linu
Android 11 获取Gps 文件的
支持多串口的选择,可以传输txt,hex格式!还可以保存为文件,serialport demo
雨松MOMO带你做软件 Android软件开发之数据的新建 储存 读取 删除源码 欢迎大家下载阅读。
poi读取大量数据会造成gc内存溢出的报错,由于垃圾回收机制无法将大量的对象及时的回收,而这些对象又会保存在内存中,会导致内存不够用的情况,这时候我们就需要使用新的方法,读取为cvs即可.此解决方案可支持千万数据的...
实现了将相册中的照片经过转化后存到云数据库中,并能从云数据库中获取该照片保存在手机指定文件夹,亲测可以用,够10个字了吧,开发工具是android studio。
1.文件存储数据使用了Java中的IO操作来进行文件的保存和读取,只不过Android在Context类中封装好了输入流和输出流的获取方法。 创建的存储文件保存在/data/data/<package>/files文件夹下。 2.操作。 保存文件内容...
嵌入式Android项目设计与开发 第六章 数据存储 ——基本文件存储 基本文件存储 基本文件存储就是以一种字符文件形式保存数据,数据呈现一种链式的状态,没有任何数据结构,也缺乏管理机制,但他是最基本的数据存储...
Android 读取手机文件夹向,指定文件夹下存放数据demo,里面还有应用版本升级的功能
主要介绍了Android开发实现读取excel数据并保存为xml的方法,涉及Android针对Excel数据读取及xml格式文件的构造与保存相关操作技巧,需要的朋友可以参考下
将数据存储到文件中并读取数据 1、新建FilePersistenceTest项目,并修改activity_main.xml中的代码,如下:(只加入了EditText,用于输入文本内容,不管输入什么按下back键就丢失,我们要做的是数据被回收之前,将它...