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目录和手机外部存储的更多相关文章

  1. Android Studio 添加Assets目录

    Android Studio 添加Assets目录: 法一: Since Android Studio uses the new Gradle-based build system, you shou ...

  2. Xamarin.Android 如何使用Assets目录下的文件

    原文:Xamarin.Android 如何使用Assets目录下的文件 个人原创,转载注明出处:http://blog.csdn.net/supluo/article/details/43672411 ...

  3. 【Android Studio安装部署系列】十五、Android studio添加Assets目录

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 概述 Android Studio新建项目时是没有assets目录,需要自己手动创建. app右键——New——Folder——Asset ...

  4. Android Studio增加assets目录、raw目录

    assets与res/raw不同 assets目录是Android的一种特殊目录,用于放置APP所需的固定文件,且该文件被打包到APK中时,不会被编码到二进制文件. Android还存在一种放置在re ...

  5. Android——数据存储:手机外部存储 SD卡存储

    xml <EditText android:layout_width="match_parent" android:layout_height="wrap_cont ...

  6. Android开发之assets目录下资源使用总结

    预前知识: Android资源文件分类: Android资源文件大致可以分为两种: 第一种是res目录下存放的可编译的资源文件: 这种资源文件系统会在R.Java里面自动生成该资源文件的ID,所以访问 ...

  7. Android Studio 没有assets目录的问题

    Where to place the assets folder in Android Studio   If you are having problems with asset files not ...

  8. Android开发学习---如何写数据到外部存储设备(sd卡),Environment.getExternalStorageDirectory,怎么获取sd卡的大小?

    本文主要介绍如何写数据到sd卡,这里主要到的技术是Environment中的方法. 1. 2.实现代码: /datasave/src/com/amos/datasave/savePasswordSer ...

  9. Android 4.0以后正确的获取外部sd卡存储目录

    刚解决这个棘手的问题 找了很久,随笔记下. 网上搜索 android 获取外部sd卡存储目录 普遍都是: 1) Environment.getExternalStorageDirectory() 这个 ...

随机推荐

  1. python走起之第一话

    Python介绍 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语言 ...

  2. UiAutomator环境搭建及详细操作

    一.环境搭建 1.1 必备条件 JDK SDK(API高于15) Eclipse(安装ADT插件) ANT(用于编译生成的jar) 安装JDK并添加环境变量 1.2 详细步骤 1.安装JDK并添加环境 ...

  3. 【转】Vim十大必备插件

    [转]Vim十大必备插件 转自:http://my.oschina.net/zhoukuo/blog/336315 Taglist taglist是一个用于显示定位程序中各种符号的插件,例如宏定义.变 ...

  4. sizeof既是关键字,又是运算符(操作符),但不是函数!

    sizeof是关键字吗 sizeof是关键字,这一点毋庸置疑.你不能将sizeof定义为任何标识符.查看C语言标准文档里的说明: sizeof是运算符(操作符)吗 C语言中,sizeof是运算符(操作 ...

  5. Deep Learning 5_深度学习UFLDL教程:PCA and Whitening_Exercise(斯坦福大学深度学习教程)

    前言 本文是基于Exercise:PCA and Whitening的练习. 理论知识见:UFLDL教程. 实验内容:从10张512*512自然图像中随机选取10000个12*12的图像块(patch ...

  6. 使用Eclipse创建maven项目

    前提:Eclipse中安装了maven插件,或者Eclipse版本在Mars以上(自集成maven) 1.new project --maven project 2.默认点击next 3.选择构建类型 ...

  7. CSS cursor属性

    介绍: 该属性规定要显示的光标的类型,该属性定义了鼠标指针放在一个元素边界范围之内的时候所用的光标的形状. 常用的属性值: default:默认光标 auto:浏览器默认的光标 pointer:光标呈 ...

  8. 【ubuntu】屏幕超时关闭后不能唤醒

    根据 http://blog.csdn.net/longshenlmj/article/details/18081167修改了"启动laptop_mode模式"gedit 的文件 ...

  9. JQuery中的id选择器含有特殊字符时,不能选中dom元素

    1.jquery类库在我们实际项目中用的很多,大家经常需要根据控件的id,获取对应的html元素.但是:当id含有特殊字符的时候,是不能选中的 2.jquery的id选择器只支持,单词.阿拉伯数字.下 ...

  10. 用c语言编写二分查找法

    二分法的适用范围为有序数列,这方面很有局限性. #include<stdio.h> //二分查找法 void binary_search(int a[],int start,int mid ...