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() 这个 ...
随机推荐
- Js获取下拉框选定项的值和文本
Js获取下拉框的值和文本网上提供了2种方法:但有些人很不负责任,他们根本没考虑到浏览器之间的差异导致的错误,导致很多新手琢磨了半天找不出错误! 下面我总结下Firefox和IE下获取下拉框选定项的值和 ...
- Nginx模块之————RTMP模块在Ubuntu上以串流直播HLS视频
Nginx的安装在Ubuntu上以串流直播HLS视频 https://www.vultr.com/docs/setup-nginx-on-ubuntu-to-stream-live-hls-video
- linux top命令
top命令参数 -h:help表示显示帮助的意思 -v:version显示版本的意思,和-h的功能一样 -u:user显示指定用户的进程,例如:top -u root -p:pid显示指定进程,例如: ...
- 讲解Python中的递归函数
本文的最重要的收获在于:尾递归是指,在函数返回的时候,调用自身本身,并且,return语句不能包含表达式. 在函数内部,可以调用其他函数.如果一个函数在内部调用自身本身,这个函数就是递归函数. 举个例 ...
- js中attr 与find 获取属性值,
一.attr的用法 1. attr(属性名) //获取属性的值(取得第一个匹配元素的属性值.通过这个方法可以方便地从第一个匹配元素中获取一个属性的值.如果元素没有相应属性,则返回 und ...
- [css]需警惕CSS3属性的书写顺序
转载张鑫旭:http://www.zhangxinxu.com/wordpress/2010/09/%E9%9C%80%E8%AD%A6%E6%83%95css3%E5%B1%9E%E6%80%A7% ...
- 深入SpringBoot:自定义PropertySourceLoader
http://www.jianshu.com/p/5206f74a4406 ********************************* 前言 上一篇文章介绍了SpringBoot的Enable ...
- 快速排序,C语言实现
排序法里比较出名的,具体的算法看下图: 这篇博客说的通俗易懂:http://blog.csdn.net/morewindows/article/details/6684558 这是快速排序的基础,用代 ...
- C#日常知识
常量: 定义常量:const; 条件运算符: 表达式1?表达式2:表达式3[如果正确则执行表达式2,不正确执行表达式3] (例如:int result; result = 5>4?100:200 ...
- Linux下安装国际版QQ (转)
原文链接:http://www.linuxidc.com/Linux/2016-09/134923.htm 说明:一开始,我在Ubuntu 16.04下安装的QQ版本是Wineqq2013SP6-20 ...