Android——课堂整理:assets目录和手机外部存储
layout文件:
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="保存资产文件到内部存储"
android:onClick="bt4_onClick"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/iv_1"
android:src="@drawable/on"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="设置图片指向内部存储"
android:onClick="bt5_onClick"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="写入外部存储文件"
android:onClick="bt6_onClick"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="读取外部存储文件"
android:onClick="bt7_onClick"/>
java类:
//保存资产文件到内部存储
public void bt4_onClick(View v)
{
try {
//操作assets目录的文件
//1.得到assetsManager
AssetManager am = getAssets();
//2.操作资产目录,边读边写入
//1)读文件到内存 inputstream
InputStream is = am.open("yuantu.png");
//2)写文件到目录 outputstream
FileOutputStream fos = openFileOutput("test.png",MODE_PRIVATE);
//先读后写
byte[] b = new byte[1024];
int i = 0;
while ((i = is.read(b))>0)
{
fos.write(b,0,i);
}
fos.close();
is.close();
Toast.makeText(MainActivity.this, "保存文件成功", Toast.LENGTH_SHORT).show();
}
catch (Exception e)
{
Toast.makeText(MainActivity.this, "保存文件出错", Toast.LENGTH_SHORT).show();
} }
//设置图片指向内部存储
public void bt5_onClick(View v)
{
//1.得到文件路径
String path = getFilesDir().getAbsolutePath()+"/test.png";
Toast.makeText(MainActivity.this, "path = "+path, Toast.LENGTH_SHORT).show();
//2.从内部存储的图片得到Bitmap,BitmapFactory.decodeFile("文件路径");
Bitmap bm = BitmapFactory.decodeFile(path);
//3.设置图片视图的图片来源
iv_1.setImageBitmap(bm);
}
//写入外部存储文件
public void bt6_onClick(View v)
{
//1.判断SD卡是否挂载
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
//得到文本框内容
String str = et_1.getText().toString();
try {
//写入
//1.构造输出流
//1)得到文件路径
//得到SD卡根目录
String path = Environment.getExternalStorageDirectory().getAbsolutePath(); //得到包名对应的目录
// String path = getExternalFilesDir("Music").getCanonicalPath();
Toast.makeText(MainActivity.this, "path = "+path, Toast.LENGTH_LONG).show();
//2)构造
FileOutputStream fos = new FileOutputStream(path+"/test.txt");
PrintStream ps = new PrintStream(fos);
ps.print(str);
ps.close();
fos.close();
Toast.makeText(MainActivity.this, "写入外部文件成功", Toast.LENGTH_SHORT).show();
}
catch (Exception e)
{
Toast.makeText(MainActivity.this, "存储文件出错", Toast.LENGTH_SHORT).show();
}
}
else
{
Toast.makeText(MainActivity.this, "SD卡没有挂载", Toast.LENGTH_SHORT).show();
}
}
//读取外部存储文件
public void bt7_onClick(View v)
{
//1.判断SD卡是否挂载
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
try {
String path = getExternalFilesDir("Music").getCanonicalPath()+"/test.txt";
FileInputStream fis = new FileInputStream(path);
byte[] b = new byte[1024];
int i = 0;
String str = "";
while ((i = fis.read(b))>0)
{
str += new String(b,0,i);
}
fis.close();
Toast.makeText(MainActivity.this, "文件内容 = "+str, Toast.LENGTH_SHORT).show();
}
catch (Exception e)
{
Toast.makeText(MainActivity.this, "读取外部文件失败", Toast.LENGTH_SHORT).show();
}
}
else
{
Toast.makeText(MainActivity.this, "SD没有挂载", Toast.LENGTH_SHORT).show();
}
}
Android——课堂整理:assets目录和手机外部存储的更多相关文章
- Android Studio 添加Assets目录
Android Studio 添加Assets目录: 法一: Since Android Studio uses the new Gradle-based build system, you shou ...
- Xamarin.Android 如何使用Assets目录下的文件
原文:Xamarin.Android 如何使用Assets目录下的文件 个人原创,转载注明出处:http://blog.csdn.net/supluo/article/details/43672411 ...
- 【Android Studio安装部署系列】十五、Android studio添加Assets目录
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 概述 Android Studio新建项目时是没有assets目录,需要自己手动创建. app右键——New——Folder——Asset ...
- Android Studio增加assets目录、raw目录
assets与res/raw不同 assets目录是Android的一种特殊目录,用于放置APP所需的固定文件,且该文件被打包到APK中时,不会被编码到二进制文件. Android还存在一种放置在re ...
- Android——数据存储:手机外部存储 SD卡存储
xml <EditText android:layout_width="match_parent" android:layout_height="wrap_cont ...
- Android开发之assets目录下资源使用总结
预前知识: Android资源文件分类: Android资源文件大致可以分为两种: 第一种是res目录下存放的可编译的资源文件: 这种资源文件系统会在R.Java里面自动生成该资源文件的ID,所以访问 ...
- Android Studio 没有assets目录的问题
Where to place the assets folder in Android Studio If you are having problems with asset files not ...
- Android开发学习---如何写数据到外部存储设备(sd卡),Environment.getExternalStorageDirectory,怎么获取sd卡的大小?
本文主要介绍如何写数据到sd卡,这里主要到的技术是Environment中的方法. 1. 2.实现代码: /datasave/src/com/amos/datasave/savePasswordSer ...
- Android 4.0以后正确的获取外部sd卡存储目录
刚解决这个棘手的问题 找了很久,随笔记下. 网上搜索 android 获取外部sd卡存储目录 普遍都是: 1) Environment.getExternalStorageDirectory() 这个 ...
随机推荐
- 解决iphone填写表单时,表单项获取焦点时往下拉屏,导致顶部标题栏下滑错位
$(function () { //解决iphone填写表单时,表单项获取焦点时往下拉屏,导致顶部标题栏下滑错位 var u = navigator.userAgent; var isiOS = !! ...
- PHP 小方法之 过滤参数
if (! function_exists ( 'parameter_filter' )) { function parameter_filter($str, $type = 'string', $f ...
- Action<>和Func<> 委托【代理】
C#中的Action<>和Func<> 其实他们两个都是委托[代理]的简写形式. 一.[action<>]指定那些只有输入参数,没有返回值的委托 Delegate的 ...
- scanf与scanf_s的区别
scanf()函数是标准C中提供的标准输入函数,用以用户输入数据 scanf_s()函数是Microsoft公司VS开发工具提供的一个功能相同的安全标准输入函数,从vc++2005开始,VS系统提供了 ...
- sphinx 源码阅读之分词,压缩索引,倒排——单词对应的文档ID列表本质和lucene无异 也是外部排序再压缩 解压的时候需要全部扫描doc_ids列表偏移量相加获得最终的文档ID
转自:http://github.tiankonguse.com/blog/2014/12/03/sphinx-token-inverted-sort.html 外部排序 现在我们的背景是有16个已经 ...
- Java 泛型,了解这些就够用了。
此文目录: Java泛型是什么? 通常的泛型的写法示例 类型擦除 为什么要使用Java泛型 通过示例了解PECS原则 一.Java泛型是什么? 官方定义 泛型是Java SE 1.5的新特性,泛型的本 ...
- WCF初探-20:WCF错误协定
WCF错误协定概述 在所有托管应用程序中,处理错误由 Exception 对象表示. 在基于 SOAP 的应用程序(如 WCF 应用程序)中,服务方法使用 SOAP 错误消息来传递处理错误信息. SO ...
- [宽度优先搜索] HDU 1372 Knight Moves
Knight Moves Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...
- Halcon 10.0 Sample:完整性检查(圆形)
* ball.hdev: Inspection of Ball Bonding * 球接合检查 Comment Time:// *核心思想:.白色区域用作自动ROI,黑色区域是目标 * .Openin ...
- Documentum常见问题11-xplore全文检索时找不到相关内容
最近帮助同事处理了一个关于全文检索的问题,随手记录下来供以后参考. 问题一 某些Cabinet下的文件可以全文检索到,但某些Cabinet下的数据全文检索不成功. 新建了一个Docbase-AADCT ...