手机外部存储的学习

activity_data2.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.example.dell.shujucunchu.SDkacunchu"
android:orientation="vertical"> <EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_5"/> <EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_6"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="读包的目录"
android:onClick="baocun3"/> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="读自定义目录"
android:onClick="baocun4"/> </LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="存包目录"
android:onClick="baocun5"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="存自定义目录"
android:onClick="baocun6"/> </LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="保存到带包名的目录"
android:onClick="baocun7"/> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="从带包名目录读取"
android:onClick="baocun8"/> </LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="保存到自定义的目录"
android:onClick="baocun9"/> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="从自定义目录读取"
android:onClick="baocun10"/> </LinearLayout>
</LinearLayout>

DataActivity2.java

package com.hanqi.test5;

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.Toast; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream; public class DataActivity2 extends AppCompatActivity { EditText et_5 ; EditText et_6 ; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_data2);
}
public void baocun5(View view)
{
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
//1、获取要存储的内容
et_5 = (EditText)findViewById(R.id.et_5); String content = et_5.getText().toString(); // String sdpath = Environment.getExternalStorageDirectory().getAbsolutePath();
//
// Toast.makeText(SDkacunchu.this, "sdpath="+sdpath, Toast.LENGTH_SHORT).show(); //参数 代表不同文件类型的子目录,如果没有就穿null String sdpath = getExternalFilesDir(null).getAbsolutePath(); Toast.makeText(DataActivity2.this, "sdpath =" + sdpath, Toast.LENGTH_SHORT).show(); //构造输出流 sdpath += "/sd"; try {
FileOutputStream fos = new FileOutputStream(sdpath); //传统模式 字节数组方式 fos.write(content.getBytes("utf-8")); fos.close();
} catch (Exception e) {
e.printStackTrace();
} } else
{
Toast.makeText(DataActivity2.this, "未发现sd卡", Toast.LENGTH_SHORT).show();
} } public void baocun3(View view)
{
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
//1、获取要存储的内容
et_6 = (EditText)findViewById(R.id.et_6); //String content = et_6.getText().toString(); // String sdpath = Environment.getExternalStorageDirectory().getAbsolutePath();
//
// Toast.makeText(SDkacunchu.this, "sdpath="+sdpath, Toast.LENGTH_SHORT).show(); //参数 代表不同文件类型的子目录,如果没有就穿null String sdpath = getExternalFilesDir(null).getAbsolutePath(); //Toast.makeText(SDkacunchu.this, "sdpath =" + sdpath, Toast.LENGTH_SHORT).show(); //构造输出流 sdpath += "/sd"; try {
FileInputStream fis = new FileInputStream(sdpath); //传统模式 字节数组方式 byte[] b = new byte[1024]; int i =0; StringBuilder str = new StringBuilder(); while ((i=fis.read(b)) > 0) { et_6.setText(str.append(new String(b, 0, i))); } fis.close();
} catch (Exception e) {
e.printStackTrace();
} } else
{
Toast.makeText(DataActivity2.this, "未发现sd卡", Toast.LENGTH_SHORT).show();
} } public void baocun6(View view)
{
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
//1、获取要存储的内容
et_5 = (EditText)findViewById(R.id.et_5); String content = et_5.getText().toString();
//获取外部存储根目录 String sdpath = Environment.getExternalStorageDirectory().getAbsolutePath();
//在sd卡根目录下再创建子目录
sdpath += "/hanqi"; File file = new File(sdpath);
//如果不存在
if (!file.exists())
{
//创建目录
file.mkdir();
//创建文件
//file.createNewFile();
}
Toast.makeText(DataActivity2.this, "sdpath="+sdpath, Toast.LENGTH_SHORT).show(); //构造输出流 sdpath += "/test.txt"; try {
FileOutputStream fos = new FileOutputStream(sdpath); //传统模式 字节数组方式 fos.write(content.getBytes("utf-8")); fos.close();
} catch (Exception e) {
e.printStackTrace();
} } else
{
Toast.makeText(DataActivity2.this, "未发现sd卡", Toast.LENGTH_SHORT).show();
} } public void baocun4(View view)
{
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
//1、获取要存储的内容
et_6 = (EditText)findViewById(R.id.et_6); //String content = et_6.getText().toString(); String sdpath = Environment.getExternalStorageDirectory().getAbsolutePath(); Toast.makeText(DataActivity2.this, "sdpath="+sdpath, Toast.LENGTH_SHORT).show(); //构造输出流 sdpath += "/hanqi/test.txt"; try {
FileInputStream fis = new FileInputStream(sdpath); //传统模式 字节数组方式 byte[] b = new byte[1024]; int i =0; StringBuilder str = new StringBuilder(); while ((i=fis.read(b)) > 0) { et_6.setText(str.append(new String(b, 0, i))); } fis.close();
} catch (Exception e) {
e.printStackTrace();
} } else
{
Toast.makeText(DataActivity2.this, "未发现sd卡", Toast.LENGTH_SHORT).show();
} } }

Android课程---关于数据存储的学习(2)的更多相关文章

  1. Android课程---关于数据存储的学习

    activity_data1.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout ...

  2. Android课程---关于数据存储的学习(3)之数据库和事务

    DataActivity3.java package com.hanqi.test5; import android.content.ContentValues; import android.dat ...

  3. Android课程---关于数据存储的学习之总结

  4. Android Learning:数据存储方案归纳与总结

    前言 最近在学习<第一行android代码>和<疯狂android讲义>,我的感触是Android应用的本质其实就是数据的处理,包括数据的接收,存储,处理以及显示,我想针对这几 ...

  5. Android中的数据存储(二):文件存储 2017-05-25 08:16 35人阅读 评论(0) 收藏

    文件存储 这是本人(菜鸟)学习android数据存储时接触的有关文件存储的知识以及本人自己写的简单地demo,为初学者学习和使用文件存储提供一些帮助.. 如果有需要查看SharedPreference ...

  6. 67.Android中的数据存储总结

    转载:http://mp.weixin.qq.com/s?__biz=MzIzMjE1Njg4Mw==&mid=2650117688&idx=1&sn=d6c73f9f04d0 ...

  7. Android中的数据存储

    Android中的数据存储主要分为三种基本方法: 1.利用shared preferences存储一些轻量级的键值对数据. 2.传统文件系统. 3.利用SQLite的数据库管理系统. 对SharedP ...

  8. Android五种数据存储方式

    android 五种数据存储 :SharePreferences.SQLite.Contert Provider.File.网络存储 Android系统提供了四种存储数据方式.分别为:SharePre ...

  9. Android下的数据存储与訪问 --- 以文件的形式

    Android下的数据存储与訪问 --- 以文件的形式 1.1 储存文件存放在手机内存中: // *** 储存数据到 /data/data/包名/files/jxn.txt文件里 String dat ...

随机推荐

  1. 【Alpha版本】冲刺-Day5

    队伍:606notconnected 会议时间:11月13日 会议总结 张斯巍(433) 今天安排:完成昨天没完成的,设置界面设计 完成度:85% 明天计划:学习UI设计 遇到的问题:无 感想:一定要 ...

  2. WPF CollectionViewSource CollectionView

    CollectionView 通俗讲就是可以对你绑定的集合可以进行 分组,排序 等功能 CollectionViewSource  根据字面意思是xxx的数据源 详细的介绍还是看 http://www ...

  3. java开发JDBC连接数据库详解

    JDBC连接数据库 好文一定要让大家看见 •创建一个以JDBC连接数据库的程序,包含7个步骤: 1.加载JDBC驱动程序: 在连接数据库之前,首先要加载想要连接的数据库的驱动到JVM(Java虚拟机) ...

  4. realloc的使用误区

    C语言 realloc() 函数位于 stdlib.h 头文件中,原型为: void *realloc(void *ptr, size_t size); realloc() 会将 ptr 所指向的内存 ...

  5. With great power comes great responsibility

    We trust you have received the usual lecture from the local SystemAdministrator. It usually boils do ...

  6. OF寄存器的判断

      1000 0000 ADD  1100 0000  10 1:符号位是否有进位  有则为1 2:最高有效数值位是否向符号位产生进位  有则为1 1 XOR 0=1所以PF=1

  7. sql 列转行

    原表:转过的表: 代码: ) set @sql = 'select AssetRecordId ' select @sql = @sql + ' , max(case ExtendName when ...

  8. string与wstring之间的转换

    #include <string>std::string ws2s(const std::wstring& ws){    std::string curLocale = setl ...

  9. Memcache之telnet操作

    在telnet Memcache之前,先要确认 memcached已启用. 如:ps -ef |grep memcache netstat -elp |grep memcache 或者 netstat ...

  10. Css Sprites 多张图片整合在一张图片上

    CSS Sprites原理: CSS Sprites其实就是把网页中一些背景图片整合到一张图片文件中,再利用CSS的"background-image","backgro ...