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() 这个 ...
随机推荐
- jquery总结05-常用事件02-表单事件
表单事件 .focus()元素获得焦点时 阻止冒泡 子元素不可以 .blur() 元素失去焦点时 阻止冒泡 子元素不可以 .change() input.select.textarea值发生改变时 i ...
- 解决服务器时间Local time和RTC time不一致的问题
今天遇到了一个问题,发现程序读取的时间比实际的时间快了几分钟. 执行 timedatectl status 发现 Local time比实际的快,但是RTC time是准确的. 各种google也没搞 ...
- WebSocket实战之————Workerman服务器的安装启动
安装php apt-get install php5-cli root@iZ23b64pe35Z:/home/www# php -v PHP 5.5.9-1ubuntu4.20 (cli) (buil ...
- Knockout学习笔记之二($root,$parent及$data的区别)
以下是我从Google上找到的一个例子,非常生动形象,我修改了部分代码,具体内容如下: 对于$root 与$parent的区别: $root refers to the view model appl ...
- CSS样式使用
<html> <head> <style type = "text/css"> .h{ ...
- laravel 中 与前端的一些事3 之使用Gulp编译less
更多关于less的使用方法,参考上一篇scss的使用,两者大同小异
- ToolStrip添加自定义的DateTimePicker
直接新建一个类,代码如下: [ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.All), DefaultEven ...
- 51nod 1459 迷宫游戏(dij)
题目链接:51nod 1459 迷宫游戏 dij裸题. #include<cstdio> #include<cstring> #include<algorithm> ...
- 读<jquery 权威指南>[3]-动画
一. 显示与隐藏——hide(),show() 1. 方法: hide(speed,[callback]); show(speed,[callback]); 说明:这两个方法还可以实现带动画效果的显示 ...
- ShopNc基本介绍
ShopNc学习笔记: 1.shopNc每个文件夹定义了单入口文件eg:shopnc/admin/index.php, shopnc/cms/index.php 2.MVC M: $model = M ...