手机内部存储外部存储

Posted 尚文韬

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了手机内部存储外部存储相关的知识,希望对你有一定的参考价值。

package com.example.lenovo.myapplication;

import android.content.SharedPreferences;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
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 MainActivity extends AppCompatActivity {

    EditText et1;
    TextView tv1;
    ImageView iv1;

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

        et1 = (EditText) findViewById(R.id.et1);
        tv1 = (TextView) findViewById(R.id.tv1);
        iv1 = (ImageView) findViewById(R.id.iv1);
    }

    public void bt_1(View v) {
        //1-得到SharedPreferences
        SharedPreferences sharedPreferences = getSharedPreferences("abc", MODE_PRIVATE);
        //2-得到编译器
        SharedPreferences.Editor editor = sharedPreferences.edit();
        //3-使用Editor添加数据
        editor.putString("a", "abcdfe");
        editor.putLong("b", 123456);
        //4-提交保存
        editor.commit();

        Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
    }

    //读取
    public void bt_2(View v) {
        SharedPreferences sharedPreferences = getSharedPreferences("abc", MODE_PRIVATE);
        String s = sharedPreferences.getString("b", null);

        Toast.makeText(MainActivity.this, "key=b" + "value=" + s, Toast.LENGTH_SHORT).show();
    }

    //写内部文件
    public void bt_3(View v) {
        //从内存里写入文件

        try {
            //1-得到内部的存储文件
            File file = getFilesDir();
            String path = file.getAbsolutePath();
            Toast.makeText(MainActivity.this, "path=" + path, Toast.LENGTH_SHORT).show();
            //2-用输出流写入文件
            FileOutputStream fileOutputStream = openFileOutput("text.txt", MODE_PRIVATE);
            //3-写入文件内容
            PrintStream printStream = new PrintStream(fileOutputStream);
            String str = et1.getText().toString();

            printStream.println(str);
            //printStream.println("自动换行");
            printStream.close();
            Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
        } catch (Exception ex) {
            Toast.makeText(MainActivity.this, "保存失败", Toast.LENGTH_SHORT).show();

        }
    }

    //
    public void bt_4(View v) {
        try {
            //输入流
            FileInputStream fis = openFileInput("text.txt");

            //1-定义byte[]
            byte[] b = new byte[1024];
            int i = 0;//读到的数据长度
            String str1 = "";
            //2-循环读
            while ((i = fis.read(b)) > 0) {
                String str = new String(b, 0, i);
                str1 += str;
            }
            fis.close();
            tv1.setText(str1);
        } catch (Exception ex) {

        }
    }

    public void bt_5(View v) {
        try {
            //操作assets目录的文件

            //1-得到assets目录管理器Manager
            AssetManager assetManager = getAssets();


            //2-操作资产目录,边读边写入
            //1)-先读文件到内存 inputstream
            InputStream inputStream = assetManager.open("abc.jpg");
            //2)-写文件到目录outputstream
            FileOutputStream fileOutputStream = openFileOutput("text.jpg", MODE_PRIVATE);

            //先读后写入
            byte[] b = new byte[1024];
            int i = 0;
            while ((i = inputStream.read(b)) > 0) {
                fileOutputStream.write(b, 0, i);
            }
            fileOutputStream.close();
            inputStream.close();
            Toast.makeText(MainActivity.this, "保存文件成功", Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            Toast.makeText(MainActivity.this, "保存文件出错", Toast.LENGTH_SHORT).show();
        }


    }

    public void bt_6(View v) {
        //3-得到文件路径
        String path = getFilesDir().getAbsolutePath() + "/text.jpg";

        Toast.makeText(MainActivity.this, "path=" + path, Toast.LENGTH_SHORT).show();
        //2-从内部存储的图片得到Bitmap  BitmapFactory.decodeFile("文件路径");
        Bitmap bm = BitmapFactory.decodeFile(path);
        //1-设置图片视图的图片数据来源
        iv1.setImageBitmap(bm);
    }

    public void bt_7(View v) {
        //1-判断SC卡是否挂载
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            //得到文本框内容
            String str = et1.getText().toString();
            try {
                //写入
                //1-构造输出流
                //1)得到文件路径。得到SD卡的根目录
                //String path=Environment.getExternalStorageDirectory().getCanonicalPath();
                //2)得到包名对应的目录
                String path = getExternalFilesDir("Musics").getCanonicalPath();

                Toast.makeText(MainActivity.this, "path=" + path, Toast.LENGTH_SHORT).show();
                //2)构造
                FileOutputStream fos = new FileOutputStream("path");
                PrintStream printStream = new PrintStream(fos);
                printStream.print(str);
                printStream.close();
                fos.close();

                Toast.makeText(MainActivity.this, "存储成功", Toast.LENGTH_SHORT).show();
            } catch (Exception e) {
                Toast.makeText(MainActivity.this, "存储失败", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(MainActivity.this, "SD没有", Toast.LENGTH_SHORT).show();
        }
    }


    public void bt_8(View v) {
        //1-判断SC卡是否挂载
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
        {
            try {
                String path = getExternalFilesDir("Music").getCanonicalPath() + "/text.jpg";
                FileInputStream fls=new FileInputStream(path);
                byte[]b=new byte[1024];
                int i=0;
                String str="";
                while ((i=fls.read(b))>0)
                {
                    str+=new String(b,0,i);
                }
                fls.close();
                Toast.makeText(MainActivity.this, "读取成功,文件内容:"+str, Toast.LENGTH_SHORT).show();
            }
            catch (Exception e)
            {
                Toast.makeText(MainActivity.this, "读取失败", Toast.LENGTH_SHORT).show();
            }

        }
        else
        {
            Toast.makeText(MainActivity.this, "SD没有", Toast.LENGTH_SHORT).show();
        }
    }

}
View Code

 

 

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.lenovo.myapplication.MainActivity"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:id="@+id/tv1"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="sp存储"
        android:onClick="bt_1"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="sp读取"
        android:onClick="bt_2"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/et1"
        android:hint="输入……"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="写内部文件"
        android:onClick="bt_3"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="读内部文件"
        android:onClick="bt_4"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="保存资产文件到内部存储"
        android:onClick="bt_5"/>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/iv1"
        android:src="@drawable/nnn"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="设置图片指向内部存储"
        android:onClick="bt_6"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="写入外部存储文件"
        android:onClick="bt_7"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="读取外部存储文件"
        android:onClick="bt_8"/>
</LinearLayout>
View Code

 

以上是关于手机内部存储外部存储的主要内容,如果未能解决你的问题,请参考以下文章

安卓内部外部文件存储

Android 内部存储/外部存储 及 读写权限

安卓内外部存储完全解析 -- 别再弄混了

Android内存解析— 详解内存,内部存储和外部存储

内部存储器与外部存储器的区别

安卓的存储机制