SharedPreferences存储数据及文件数据存储

一.什么是内部、外部存储

在这里插入图片描述

二.权限

 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

6.0之后的动态权限

private String[] permissions = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.INTERNET};
/**
         * 6.0以上加动态权限
         */
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            ActivityCompat.requestPermissions(this,permissions,100);
        }            

三.内部存储

在这里插入图片描述

获得路径方法

context.getCacheDir()
context.getFilesDir()

SharedPreferences存储

public void writeShare(){
        SharedPreferences sharedPreferences = getSharedPreferences("1804A",MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean("istrue",false);
        editor.putFloat("float",0);
        editor.putInt("int",1);
        editor.putLong("long",1);
        editor.putString("string",null);
        Set<String> stringSet = new HashSet<String>();
        stringSet.add("张三");
        stringSet.add("李四");
        editor.putStringSet("set",stringSet);
        editor.commit();
    }
public void readShare(){
        SharedPreferences sharedPreferences = getSharedPreferences("1804A",MODE_PRIVATE);
        boolean result = sharedPreferences.getBoolean("istrue",true);
        //其他方法一样都使用getxxx来获得
    }

SharedPreferences读写方式

  • MODE_PRIVATE:本程序可以访问
  • MODE_WORLD_READABLE:允许其他应用程序读取文件(可读)
  • MODE_WORLD_WRITEABLE:允许其他应用程序修改文件(可写)

四.外部存储

在这里插入图片描述

获得公共目录方法

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)

文件读写

 /**
     * 写文件
     */
    public void writeFile(){
        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
            String path = Environment.getExternalStorageDirectory().getPath();
            String abPath = Environment.getExternalStorageDirectory().getAbsolutePath();
            File file = new File(path+"/test.txt");
            FileOutputStream outputStream = null;
            try {
                outputStream = new FileOutputStream(file);
                outputStream.write("你好".getBytes());
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                try {
                    if(outputStream != null)
                        outputStream.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }
    }
/**
     * 读文件
     */
    public void readFile(){
        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
        {
            File file = Environment.getExternalStorageDirectory();
            File newFile = new File(file,"test.txt");
            StringBuffer stringBuffer = new StringBuffer();
            try {
                FileInputStream inputStream = new FileInputStream(newFile);
                byte[] bytes = new byte[1024];
                int len = 0;
                while ((len = inputStream.read(bytes)) != -1)
                {
                    stringBuffer.append(new String(bytes,0,len));
                }
                Log.d("amy",stringBuffer.toString());
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
/**
     * 下载图片保存
     */
    public void saveImage(){
        InputStream inputStream = null;
        FileOutputStream outputStream = null;
        HttpURLConnection connection = null;
        try {
            URL url = new URL(StrintImagePath);
            connection = (HttpURLConnection) url.openConnection();
            //连接成功
            if(connection.getResponseCode() == 200)
            {
                inputStream  = connection.getInputStream();
                byte[] bytes = new byte[1024];
                int len = 0;

                if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
                    File file = Environment.getExternalStorageDirectory();
                    //D/amy: /storage/emulated/0
                    File newFile = new File(file,"image.png");
                    outputStream = new FileOutputStream(newFile);
                    while ((len = inputStream.read(bytes)) != -1){
                        outputStream.write(bytes,0,len);
                    }
                }
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            try {
                if(inputStream != null)
                    inputStream.close();
                if(outputStream != null)
                    outputStream.close();
                if(connection!= null)
                    connection.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
 /**
     * 读图片
     */
    public void readImage(){
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            File file = Environment.getExternalStorageDirectory();
            File newFile = new File(file,"image.png");
            try {
                FileInputStream inputStream = new FileInputStream(newFile);
                Bitmap bitmap = BitmapFactory.decodeFile(newFile.getAbsolutePath());
                imageView.setImageBitmap(bitmap);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
    }

猜你喜欢

转载自blog.csdn.net/amynn/article/details/106887796