0
点赞
收藏
分享

微信扫一扫

【Linux】冯诺依曼体系和操作系统:

王传学 2023-05-30 阅读 79

一、存储空间

对Andriod来说,存储空间分为内部存储空间和外部存储空间。

外部存储空间也就是常说的SD卡,可以分为私有存储空间和公共存储空间。

内部存储空间和外部存储空间的私有存储空间,都是每个APP独有的,不允许其他APP访问。

公共存储空间则是所有APP都可以访问,空间也更大,可以用于存储一些大的音频文件。

那么很自然地可以得出,当APP卸载后,内部存储空间和外部存储空间的私有存储空间的文件都被清空了,但公共存储空间的文件不会被删除。

二、在Andriod中存储文本文件

在这三个空间存储,需要获得不同的路径:

//外部存储私有空间
dir = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).toString();
//外部存储公共空间
dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString();
//内部存储空间
dir = getFilesDir().toString();

其中读写外部存储公共空间时,需要修改APP权限:

 

完整代码:

package com.example.study06;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Environment;
import android.util.Log;

import com.example.study06.util.FileUtil;

import java.io.File;
import java.io.IOException;

public class FileWriteActivity extends AppCompatActivity {

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

        writeFile();
        //readFile();
    }

    

    protected void writeFile(){
        String text = "text for inner";
        String path = null;
        String fileName = System.currentTimeMillis() + ".txt";
        String dir = null;
        //外部存储私有空间
        dir = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).toString();
        //外部存储公共空间
        dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString();
        //内部存储空间
        dir = getFilesDir().toString();

        path = dir + File.separator + fileName;

        FileUtil.saveText(path,text);
        Log.d("test", path);

    }

    private void readFile() {
        String path = "/storage/emulated/0/Android/data/com.example.study06/files/Download/1685064570930.txt";
        String text = null;
        try {
            text = FileUtil.readText(path);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        Log.d("output ", text);
    }
    
}

读写工具类:

package com.example.study06.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class FileUtil {

    public static void saveText(String path, String text){
        BufferedWriter writer = null;
        try {
            writer = new BufferedWriter(new FileWriter(path));
            writer.write(text);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            if(writer!=null){
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    public static String readText(String path) throws IOException {
        BufferedReader reader = null;
        StringBuilder text = new StringBuilder();
        try {
            reader = new BufferedReader(new FileReader(path));
            String line = null;
            while((line=reader.readLine())!=null){
                text.append(line);
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }finally {
            if(reader!=null){
                try{
                    reader.close();
                }catch (IOException e){
                    e.printStackTrace();
                }

            }
        }
        return text.toString();
    }


}

结果:在对应路径可以看到文件

 

三、外部存储空间储存图片

和之前读写文本一样,在工具类中用文件的输入输出Stream流来编写读写函数:


    public static void saveImage(String path, Bitmap bm){
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(path);
            bm.compress(Bitmap.CompressFormat.PNG, 90,  out);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }finally {
            if(out!=null){
                try {
                    out.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    public static Bitmap readImage(String path){
        FileInputStream in = null;
        Bitmap bt = null;
        try {
            in = new FileInputStream(path);
            bt = BitmapFactory.decodeStream(in);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }finally {
            if(in!=null){
                try {
                    in.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return bt;
    }

 然后使用:

package com.example.study06;

import static android.util.Log.d;
import static android.webkit.ConsoleMessage.MessageLevel.LOG;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.example.study06.util.FileUtil;

import java.io.File;

public class ImageWriteActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btn_save;
    private Button btn_read;
    private ImageView iv;
    private  String path;

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

        btn_save = findViewById(R.id.btn_save);
        btn_read = findViewById(R.id.btn_read);
        iv = findViewById(R.id.iv);

        btn_save.setOnClickListener(this);
        btn_read.setOnClickListener(this);


    }

    @Override
    public void onClick(View view) {

        switch(view.getId()){
            case R.id.btn_save:
                String fileName = System.currentTimeMillis() + ".png";
                path = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).toString() + File.separator + fileName;

                Bitmap bt = BitmapFactory.decodeResource(getResources(), R.drawable.img);

                FileUtil.saveImage(path, bt);
                Log.d("test ", path);
                break;
            case R.id.btn_read:

                //method 1
                //Bitmap bt_r = FileUtil.readImage(path);
                //iv.setImageBitmap(bt_r);

                //method 2
                //Bitmap bt_r = BitmapFactory.decodeFile(path);
                //iv.setImageBitmap(bt_r);
                
                //method 3
                iv.setImageURI(Uri.parse(path));
                break;
        }
    }
}
举报

相关推荐

0 条评论