手机外部存储练习

首先记住加上读取权限

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.dell.wodelianxi">
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".sdkacunchu">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Gridview">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".baseadaptershixian">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Tupianlunbo">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".denglucunchu">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

</manifest>

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"
    tools:context="com.example.dell.wodelianxi.sdkacunchu"
    android:orientation="vertical">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="输入内容"
        android:id="@+id/et_shu"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/et_du"
        android:hint="读取内容"/>

    <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="baocun"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="读取目录"
            android:onClick="duqu"/>
    </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="baocunzidingyi"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="读取自定义目录"
        android:onClick="duquzidingyi"/>
</LinearLayout>

</LinearLayout>

JAVA代码

package com.example.dell.wodelianxi;

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 sdkacunchu extends AppCompatActivity {
    EditText et_shu;
    EditText et_du;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sdkacunchu);

    }

    public void baocun(View view)
    {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
        {
            String path = getExternalFilesDir(null).getAbsolutePath();

            path +="/tt";

            et_shu = (EditText)findViewById(R.id.et_shu);

            String content = et_shu.getText().toString();

            try {
                FileOutputStream fos = new FileOutputStream(path);

                fos.write(content.getBytes("utf-8"));

                fos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        else
        {
            Toast.makeText(sdkacunchu.this, "未发现sd卡", Toast.LENGTH_SHORT).show();
        }

    }

    public void duqu(View v)
    {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
        {
            String path = getExternalFilesDir(null).getAbsolutePath();

            path += "/tt";

            try {
                FileInputStream fis = new FileInputStream(path);

                byte[] b = new byte[1024];
                int i =0;
                StringBuilder str =new StringBuilder();
                while((i=fis.read(b))>0)
                {
                    str.append(new String(b,0,i));
                }

                et_du= (EditText)findViewById(R.id.et_du);

                et_du.setText(str);
                fis.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        else
        {
            Toast.makeText(sdkacunchu.this, "未发现sd卡", Toast.LENGTH_SHORT).show();
        }
    }

    public void baocunzidingyi(View v)
    {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
        {
            String path = Environment.getExternalStorageDirectory().getAbsolutePath();
            path += "/1126";

            et_shu = (EditText)findViewById(R.id.et_shu);

            String context = et_shu.getText().toString();

            File file = new File(path);

            if (!file.exists())
            {
                file.mkdir();
            }

            path += "/tt";

            try {
                FileOutputStream fos = new FileOutputStream(path);

               fos.write(context.getBytes("utf-8"));

                fos.close();

            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        else
        {
            Toast.makeText(sdkacunchu.this, "未发现sd卡", Toast.LENGTH_SHORT).show();
        }
    }

    public void duquzidingyi(View v)
    {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
        {
            String path = Environment.getExternalStorageDirectory().getAbsolutePath();

            path +="/1126/tt";

            et_du = (EditText)findViewById(R.id.et_du);

            try {
                FileInputStream fis = new FileInputStream(path);
                byte[] b = new byte[1024];
                int i =0;
                StringBuilder str = new StringBuilder();
                while ((i=fis.read(b))>0)
                {
                    str.append(new String(b,0,i));
                }

                et_du.setText(str);

                fis.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        else
        {
            Toast.makeText(sdkacunchu.this, "未发现sd卡", Toast.LENGTH_SHORT).show();
        }
    }
}
时间: 2024-08-06 11:51:03

手机外部存储练习的相关文章

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

xml <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/et_5" android:hint="要存储的的内容"/> <EditText android:layout_width="match_parent" android:layout_

Android——课堂整理:assets目录和手机外部存储

layout文件: 1 <Button 2 android:layout_width="match_parent" 3 android:layout_height="wrap_content" 4 android:text="保存资产文件到内部存储" 5 android:onClick="bt4_onClick"/> 6 <ImageView 7 android:layout_width="wrap

数据存储——手机外部文件存储

一.特点 1.把文件存储在手机外部存储空间(SD卡)里 2.存储的是任意类型的文件 3.使用IO输入输出流操作文件 4.文件路径 1-SD卡根目录/Android/data/包名/files/[ 文件类型],应用卸载后,数据同时被删除: 2-SD卡根目录/,应用卸载之后,数据不会被同时删除. 5.需要声明权限 1-android.permission.WRITE_EXTERNAL_STORAGE,写入文件: 2-MOUNT_UNMOUNT_FILESYSTEMS,创建和删除文件. 二.API 1

手机外部文件存储(SD卡存储)

package com.atguigu.l04_datastorage; import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException; import android.app.Activity;

Android Environment.getExternalStorageDirectory() 获取的是内部存储还是外部存储?

这几天在做Android应用的远程更新功能,将下载的更新包放在移动设备上指定的文件夹. 用的是  Environment.getExternalStorageDirectory() 这种方法.然后在获取的文件夹中新建一个hkapp文件夹,用来存放下载的apk文件. 那么,这个hkapp文件究竟是在那块存储区域呢? 一開始,看看网上的API,已经这种方法的字面意思.想当然地以为它就是获取SD卡上的文件夹,而不是手机的内部存储. 当然.除了望文生义之外,似乎还有确凿的证据支持我的观点.那就是在执行的

Android 在外部存储读写文件

本文主要介绍android中如何在外部存储读写数据 sd卡的路径 sdcard:2.3之前的sd卡路径 mnt/sdcard:4.3之前的sd卡路径 storage/sdcard:4.3之后的sd卡路径 打开file explorer 可以看到sdcard是个空的文件夹,因为这个文件夹是个快捷方式,指向/storag文件夹,接着打开storag文件夹 读写sd卡 最简单的打开sd卡的方式 File file = new File("sdcard/info.txt"); * 写sd卡需要

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&qu

彻底理解android中的内部存储与外部存储

我们先来考虑这样一个问题: 打开手机设置,选择应用管理,选择任意一个App,然后你会看到两个按钮,一个是清除缓存,另一个是清除数据,那么当我们点击清除缓存的时候清除的是哪里的数据?当我们点击清除数据的时候又是清除的哪里的数据?读完本文相信你会有答案. 在android开发中我们常常听到这样几个概念,内存,内部存储,外部存储,很多人常常将这三个东西搞混,那么我们今天就先来详细说说这三个东西是怎么回事? 内存,我们在英文中称作memory,内部存储,我们称为InternalStorage,外部存储我

【转】 android中的文件操作详解以及内部存储和外部存储

摘要 其实安卓文件的操作和Java在pc环境下的操作并无二致,之所以需要单独讲解是因为安卓系统提供了不同于pc的访问文件系统根路径的api,同时对一个应用的私有文件做了统一的管理.根据我的经验,初学者在这部分感到很容易混淆内部存储和外部存储两个概念. 相对 其实安卓文件的操作和java在pc环境下的操作并无二致,之所以需要单独讲解是因为安卓系统提供了不同于pc的访问文件系统根路径的api,同时对一个应用的私有文件做了统一的管理.根据我的经验,初学者在这部分感到很容易混淆内部存储和外部存储两个概念