码迷,mamicode.com
首页 > 移动开发 > 详细

手机外部存储练习

时间:2016-04-12 00:27:44      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:

首先记住加上读取权限

<?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();
        }
    }
}

 

手机外部存储练习

标签:

原文地址:http://www.cnblogs.com/youshashuosha/p/5380735.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!