zoukankan      html  css  js  c++  java
  • 手机外部存储练习

    首先记住加上读取权限

    <?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();
            }
        }
    }
  • 相关阅读:
    【唯星宠物】——CSS/BootStrap/Jquery爬坑之响应式首页
    【可用性评估】——手机输入法可用性评估·论文
    一个简单示例看懂.Net 并行编程
    CentOS 7.1上安装.Net Core
    用 QGIS 画矢量交通路线图
    工作流服务实战
    JVM调优总结
    内存调优
    ConcurrentHashMap原理分析
    Mac上安装go环境
  • 原文地址:https://www.cnblogs.com/youshashuosha/p/5380735.html
Copyright © 2011-2022 走看看