andorid 手机外部储存
.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.hanqi.application3.DataActivity1"
android:orientation="vertical"> <EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_1"
android:hint="Key"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_2"
android:hint="Value"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="保存"
android:layout_weight="1"
android:onClick="bt1_onClick"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="读取"
android:layout_weight="1"
android:onClick="bt2_onClick"/> </LinearLayout>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_3"
android:hint="要保存的内容"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_4"
android:hint="从文件中读取的内容"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="保存"
android:layout_weight="1"
android:onClick="bt3_onClick"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="读取"
android:layout_weight="1"
android:onClick="bt4_onClick"/> </LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="保存文件"
android:layout_weight="1"
android:onClick="bt5_onClick"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="读取文件"
android:layout_weight="1"
android:onClick="bt6_onClick"/> </LinearLayout> <ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:src="@drawable/da1"
android:id="@+id/iv_4"
android:visibility="gone"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="保存到带包名的目录"
android:layout_weight="1"
android:onClick="bt7_onClick"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="从带包名目录读取"
android:layout_weight="1"
android:onClick="bt8_onClick"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="保存到自定义目录"
android:layout_weight="1"
android:onClick="bt9_onClick"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="从自定义目录读取"
android:layout_weight="1"
android:onClick="bt10_onClick"/>
</LinearLayout> </LinearLayout>
.java 9和10
package com.hanqi.application3; import android.content.SharedPreferences;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.PrintStream; public class DataActivity1 extends AppCompatActivity {
EditText et1;
EditText et2;
EditText et3;
EditText et4; ImageView iv4;
SharedPreferences sp; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_data1); et1=(EditText)findViewById(R.id.et_1);
et2=(EditText)findViewById(R.id.et_2); et3=(EditText)findViewById(R.id.et_3);
et4=(EditText)findViewById(R.id.et_4);
iv4= (ImageView)findViewById(R.id.iv_4);
//1.获取sp的实例,制定了文件名和操作模式 MODE_PRIVATE私有
sp = getSharedPreferences("mydata",MODE_PRIVATE); }
//操作assets内的文件
public void bt5_onClick(View v)
{
//1.获取AssetManager
AssetManager am = getAssets(); try{
//2.打开文件,获取输入流
InputStream is = am.open("denglu.jpg");
//3.获取输出流
FileOutputStream fos2 = openFileOutput("denglu2.jpg",MODE_PRIVATE);
//4.边度编写
byte[] bb2 = new byte[1024]; int ii2 = 0;
while ((ii2 = is.read(bb2))>0)
{
fos2.write(bb2,0,ii2);
} fos2.close(); is.close(); Toast.makeText(DataActivity1.this, "保存成功", Toast.LENGTH_SHORT).show(); }
catch (Exception e)
{
e.printStackTrace();
Toast.makeText(DataActivity1.this, "保存失败", Toast.LENGTH_SHORT).show();
}
}
//从手机内部存储读图片文件
public void bt6_onClick(View v)
{
//改变ImageView的图片来源,指向手机存储空间 //1.获取文件存储的绝对路径
String filepath = getFilesDir().getAbsolutePath();
//2.组合完整路径
filepath += "/denglu2.jpg";
//3.生成位图实例
Bitmap bm2 = BitmapFactory.decodeFile(filepath);
//4.改变ImageView的图片来源
iv4.setImageBitmap(bm2); }
//文件名
final String FILENAME = "test.txt"; public void bt3_onClick(View v)
{
//1.获取要存储的内容
String content = et3.getText().toString();
//2.获取输出流
try {
FileOutputStream fos1 = openFileOutput(FILENAME, MODE_APPEND);
//3.构造一个打印输出流
PrintStream pm1 = new PrintStream(fos1);
//4.写入内容
pm1.println(content); pm1.close(); fos1.close(); Toast.makeText(DataActivity1.this, "保存成功", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace(); Toast.makeText(DataActivity1.this, "保存失败", Toast.LENGTH_SHORT).show();
} } public void bt4_onClick(View v)
{
try {
//1.获取输入流
FileInputStream fis1 = openFileInput(FILENAME);
//2.定义读取的数组
byte[] bt11 = new byte[1024];
//3.读出数据的长度 int ii = 0; StringBuilder bb1 = new StringBuilder ();
while ((ii = fis1.read(bt11))>0)
{
bb1.append(new String(bt11,0, ii));
} fis1.close();
//设置显示读出的内容
et4.setText(bb1); Toast.makeText(DataActivity1.this, "显示成功", Toast.LENGTH_SHORT).show();
}
catch (Exception e)
{
e.printStackTrace(); Toast.makeText(DataActivity1.this, "保存失败", Toast.LENGTH_SHORT).show(); } } //保存
public void bt1_onClick(View v)
{
//1.获取Key和Value
String key = et1.getText().toString();
String value = et2.getText().toString();
if (key.length() ==0||value.length() == 0)
{
Toast.makeText(DataActivity1.this, "key或value不能为空", Toast.LENGTH_SHORT).show();
}
else { //2.取得Editor edit编辑器
SharedPreferences.Editor editor = sp.edit();
//3.放入键值对
editor.putString(key,value);
//4.提交保存
boolean b = editor.commit();
if (b)
{
Toast.makeText(DataActivity1.this, "保存成功", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(DataActivity1.this, "保存失败", Toast.LENGTH_SHORT).show();
}
} }
//读取
public void bt2_onClick(View v)
{ //1.获取要读的key
String key = et1.getText().toString();
//2.读并设置文本框
et2.setText(sp.getString(key,"没有发现key")); } //向外部存储空间存储文件 保存到带包名的目录
public void bt7_onClick(View v)
{
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { //1.获取要存储的内容
String content = et3.getText().toString();
//2.获取外部带包名的目录
//String sdPath = Environment.getExternalStorageDirectory().getAbsolutePath();
//Toast.makeText(DataActivity1.this, "sdPath= "+sdPath, Toast.LENGTH_SHORT).show();
//参数 代表不同文件类型的子目录,如果没有,就传null
String sdPath = getExternalFilesDir(null).getAbsolutePath();
Toast.makeText(DataActivity1.this, "sdPath = "+sdPath, Toast.LENGTH_SHORT).show();
//3.构造输出流
sdPath += "/" +FILENAME;
try {
FileOutputStream fos3 = new FileOutputStream(sdPath); //传统方式 字节数组方式 fos3.write(content.getBytes("utf-8")); fos3.close(); Toast.makeText(DataActivity1.this, "保存成功", Toast.LENGTH_SHORT).show(); }
catch (Exception e)
{
e.printStackTrace();
Toast.makeText(DataActivity1.this, "保存失败", Toast.LENGTH_SHORT).show(); } }
else
{
Toast.makeText(DataActivity1.this, "SD卡没有挂载", Toast.LENGTH_SHORT).show();
} } //从外部存储空间读取文件 带包名的目录
public void bt8_onClick(View v) {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { //1.获取要存储的内容
//String content = et3.getText().toString();
//2.获取外部带包名的目录
//String sdPath = Environment.getExternalStorageDirectory().getAbsolutePath();
//Toast.makeText(DataActivity1.this, "sdPath= "+sdPath, Toast.LENGTH_SHORT).show();
//参数 代表不同文件类型的子目录,如果没有,就传null
String sdPath = getExternalFilesDir(null).getAbsolutePath();
//Toast.makeText(DataActivity1.this, "sdPath = "+sdPath, Toast.LENGTH_SHORT).show();
//3.构造输入流
sdPath += "/" + FILENAME;
try {
FileInputStream fis3 = new FileInputStream(sdPath); byte[] b = new byte[1024];
int i = 0;
StringBuilder sb1 = new StringBuilder(); while ((i = fis3.read(b)) > 0) {
sb1.append(new String(b, 0, i));
} et4.setText(sb1); Toast.makeText(DataActivity1.this, "读取成功", Toast.LENGTH_SHORT).show(); } catch (Exception e) {
e.printStackTrace();
Toast.makeText(DataActivity1.this, "读取失败", Toast.LENGTH_SHORT).show(); } } else {
Toast.makeText(DataActivity1.this, "SD卡没有挂载", Toast.LENGTH_SHORT).show();
} }
//向外部存储空间存储文件 保存到自定义的目录
public void bt9_onClick(View v)
{
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { //1.获取要存储的内容
String content = et3.getText().toString();
//2.获取外部存储的根目录
String sdPath = Environment.getExternalStorageDirectory().getAbsolutePath();
//在sd卡的根目录下,创建子目录
sdPath += "/hanqi";
//实例化File 指向一个目录
File file = new File(sdPath);
//如果不存在
if(!file.exists())
{
//创建目录
file.mkdirs();
} Toast.makeText(DataActivity1.this, "sdPath= "+sdPath, Toast.LENGTH_SHORT).show(); //3.构造输出流
sdPath += "/" +FILENAME;
try {
FileOutputStream fos3 = new FileOutputStream(sdPath); //传统方式 字节数组方式 fos3.write(content.getBytes("utf-8")); fos3.close(); Toast.makeText(DataActivity1.this, "保存成功", Toast.LENGTH_SHORT).show(); }
catch (Exception e)
{
e.printStackTrace();
Toast.makeText(DataActivity1.this, "保存失败", Toast.LENGTH_SHORT).show(); } }
else
{
Toast.makeText(DataActivity1.this, "SD卡没有挂载", Toast.LENGTH_SHORT).show();
} } //从外部存储空间读取文件 带包名的目录
public void bt10_onClick(View v) {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { //1.获取要存储的内容
//String content = et3.getText().toString();
//2.获取外部带包名的目录
String sdPath = Environment.getExternalStorageDirectory().getAbsolutePath();
//Toast.makeText(DataActivity1.this, "sdPath= "+sdPath, Toast.LENGTH_SHORT).show();
//参数 代表不同文件类型的子目录,如果没有,就传null
//String sdPath = getExternalFilesDir(null).getAbsolutePath();
//Toast.makeText(DataActivity1.this, "sdPath = "+sdPath, Toast.LENGTH_SHORT).show();
//3.构造输入流
sdPath += "/hanqi"+"/" + FILENAME;
try {
FileInputStream fis3 = new FileInputStream(sdPath); byte[] b = new byte[1024];
int i = 0;
StringBuilder sb1 = new StringBuilder(); while ((i = fis3.read(b)) > 0) {
sb1.append(new String(b, 0, i));
} et4.setText(sb1); Toast.makeText(DataActivity1.this, "读取成功", Toast.LENGTH_SHORT).show(); } catch (Exception e) {
e.printStackTrace();
Toast.makeText(DataActivity1.this, "读取失败", Toast.LENGTH_SHORT).show(); } } else {
Toast.makeText(DataActivity1.this, "SD卡没有挂载", Toast.LENGTH_SHORT).show();
} }
}
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
权限
andorid 手机外部储存的更多相关文章
- andorid 数据储存、SharedPreferences存储和手机内部储存
.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android ...
- Android——课堂整理:assets目录和手机外部存储
layout文件: <Button android:layout_width="match_parent" android:layout_height="wrap_ ...
- mono for andorid 引用外部的dll问题
这几天玩mono for android 心想,咱c#终于可以开发移动应用了,心里那个美啊------------ 先开发个什么呢,想起来前几天看到微博里一个用姓名笔画来算两个人关系的小测试,开发个这 ...
- Andorid手机振动器(Vibrator)的使用
标签: android vibrator 震动器 it 分类: Andorid 获取振动器Vibrator实例: Vibrator mVibrator = (Vibrator) context.ge ...
- Android——数据存储:手机外部存储 SD卡存储
xml <EditText android:layout_width="match_parent" android:layout_height="wrap_cont ...
- andorid手机电脑操作
之前一直使用androidscreencast在pc上对手机进行操作,好久都没用了,前些天再次用的时候,提演示样例如以下: 决定还是自己写一个吧,由于7月份要做一个小分享,打算讲一些android的东 ...
- Andorid 手机WIFI连接的Mac地址和IP地址
public static String getInfo() { WifiManager wifi = (WifiManager) getSystemService(Contex ...
- Android Environment.getExternalStorageDirectory() 获取的是内部存储还是外部存储?
这几天在做Android应用的远程更新功能,将下载的更新包放在移动设备上指定的文件夹. 用的是 Environment.getExternalStorageDirectory() 这种方法.然后在获 ...
- Android储存
Android储存一共5种方法 一: 手机内置,外部储存 1.获取本地存储 (Android的读写文件及权限设置) getFilesDir() data/data/包名/File getCache ...
随机推荐
- 新书预告 ArcGIS跨平台开发系列第一本
新书预告 ArcGIS跨平台开发系列第一本 候选题目: ArcGIS Runtime开发实验实习教程 ArcGIS Runtime开发案例教程 简介: GIS最新现代开发理念打造的跨所有移动和桌面平台 ...
- [图解tensorflow源码] 入门准备工作附常用的矩阵计算工具[转]
[图解tensorflow源码] 入门准备工作 附常用的矩阵计算工具[转] Link: https://www.cnblogs.com/yao62995/p/5773142.html tensorf ...
- nagiosQL访问时报错PHP message: PHP Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead
nagiosQL安装环境: CentOS release 6.4 (Final) php-5.5.4 nagiosql_320 nginx version: nginx/1.2.3 安装一切正常,当访 ...
- Python自动化运维开发实战 二、Python基本用法
导语: Python编程博大精深,知识点众多,需要先整体上了解python的一些基本用法之后再去对每一个知识点细细研究,这样学习的速度会快很多.所以下面就先看一些python事先需要知道的基本知识. ...
- OpenCV Python : No drawMatchesknn function
2 down vote The functions cv2.drawMatches and cv2.drawMatchesKnn are not available in newer versions ...
- css flex 兼容ios android--商品展示 添加购物车
https://blog.csdn.net/u010035608/article/details/52711248 <!DOCTYPE html> <html> <hea ...
- Java多态的向上转型和向下转型
Java多态的向上转型和向下转型 向上转型:(子类转为父类,自动转型) 格式 :父类类型 变量名 = new 子类类型(); 注:子类赋值给父类,那这时这个父类就能调用子类的方法 向下转型:(父类转为 ...
- oracle导入大sql文件
最近遇到一个需要导入大SQL文件的问题,最先直接用SQL developer 导入大SQL文件,结果报IO Exception,只好采用sqlplus 导入,操作过程如下: sqlplus 用户名/密 ...
- day13 多个装饰器叠加 生成式
1.装饰器剩余 from functions import wraps @wrap(func) 会把func内的自带方法赋给wrapper,这样wrapper装饰函数就和原函数一模一样 多个装饰器叠加 ...
- NumPy 迭代数组
NumPy 迭代数组 NumPy 迭代器对象 numpy.nditer 提供了一种灵活访问一个或者多个数组元素的方式. 迭代器最基本的任务的可以完成对数组元素的访问. 接下来我们使用 arange() ...