Android开发之数据持久化存储三

Android开发之数据持久化存储三,第1张

概述一、目标实现SD卡的文件存储,SD卡是否挂载的检查和SD卡剩余空间的查询二、源程序代码packagecom.example.qq_logindemo;importandroid.Manifest;importandroid.app.Activity;importandroid.content.pm.PackageManager;importandroid.os.Build;importandroid.os.Bu 一、目标实现SD卡的文件存储,SD卡是否挂载的检查和SD卡剩余空间的查询二、源程序代码

package com.example.qq_logindemo;import androID.Manifest;import androID.app.Activity;import androID.content.pm.PackageManager;import androID.os.Build;import androID.os.Bundle;import androID.os.Environment;import androID.text.format.Formatter;import androID.util.Log;import androID.vIEw.VIEw;import androID.Widget.button;import androIDx.annotation.Nullable;import java.io.file;import java.io.fileOutputStream;public class SDCardDemoActivity extends Activity implements VIEw.OnClickListener {    private button writeDataBtn;    private static final String TAG="SDCardDemoActivity";    private button checkSDCardBtn;    private button getFreeSDCardBtn;    @OverrIDe    protected voID onCreate(@Nullable Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_sd_card);        if (Build.VERSION.SDK_INT>=23) {            int REQUEST_CODE_CONTACT = 101;            String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE};            //验证是否许可权限            for (String str : permissions) {                if (this.checkSelfPermission(str) != PackageManager.PERMISSION_GRANTED) {                    //申请权限                    this.requestPermissions(permissions, REQUEST_CODE_CONTACT);                    return;                }            }        }        initVIEw();        initOnClickListener();    }    private voID initOnClickListener() {        writeDataBtn.setonClickListener(this);        checkSDCardBtn.setonClickListener(this);        getFreeSDCardBtn.setonClickListener(this);    }    private voID initVIEw() {        getFreeSDCardBtn = this.findVIEwByID(R.ID.btn_get_free_sd_card);        writeDataBtn = this.findVIEwByID(R.ID.btn_write_2_sd_card);        checkSDCardBtn = this.findVIEwByID(R.ID.btn_check_sd_card);    }    @OverrIDe    public voID onClick(VIEw v) {        if (v==writeDataBtn) {            //存储到SD卡            file externalStorageDirectory = Environment.getExternalStorageDirectory();            Log.d(TAG, "已经存储到 ---> "+externalStorageDirectory.toString());            file filePath =new file("/storage/self/primary");            file file =new file(filePath,"info.txt");            try {                fileOutputStream fos=new fileOutputStream(file);                fos.write("heihei".getBytes());                fos.close();            } catch (Exception e) {                e.printstacktrace();            }        }        else if (v == checkSDCardBtn) {            //检查SD卡是否挂载            String state = Environment.getExternalStorageState();            if(state.equals(Environment.MEDIA_MOUNTED)){                Log.d(TAG, "SD卡已经挂载");            }else if (state.equals(Environment.MEDIA_UNMOUNTED)){                Log.d(TAG, " SD卡已经删除");            }        }        else if(v==getFreeSDCardBtn){            file exfile = Environment.getExternalStorageDirectory();            Log.d(TAG, "onClick: file Path ==="+exfile.toString());            long freeSpace =exfile.getFreeSpace();            //转换单位,把long转化成直观的  kb,Gb等            String freeText= Formatter.formatfileSize(this,freeSpace);            Log.d(TAG, "onClick: SD卡空余空间为:"+freeText);        }    }}
SDCardDemoActivity

<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:orIEntation="vertical" androID:layout_wIDth="match_parent"    androID:layout_height="match_parent">    <button        androID:ID="@+ID/btn_write_2_sd_card"        androID:layout_wIDth="match_parent"        androID:text="存储到SD卡"        androID:layout_height="wrap_content"/>    <button        androID:ID="@+ID/btn_check_sd_card"        androID:layout_wIDth="match_parent"        androID:text="检查SD卡是否挂载"        androID:layout_height="wrap_content"/>    <button        androID:ID="@+ID/btn_get_free_sd_card"        androID:layout_wIDth="match_parent"        androID:text="SD卡剩余空间"        androID:layout_height="wrap_content"/></linearLayout>
activity_sd_card

 

三、效果

 

 

 

 四、心得体会今天的学习给我留下了很深的印象,我了解到了AndroID系统在开发的时候向系统申请的权限十分重要!AndroID6.0以及6.0+往上的版本除了需要在ManiFest里面写下权限,还需要再activity里面写下动态的权限申请代码才行。由于我看的视频里面的模拟器版本和我的不太一样所以在寻找SD卡路径的时候非常费劲,不过有一个方法虽然已经过时了,但是还能用,可以直接获取SD卡的Path路径,即Environment.getExternalStorageDirectory()。同时我又发现了一个困扰我很久的问题的答案,就是为什么编译器提醒的同名方法,名字相同只是后面括号中包括的内容不同在之后书写代码的时候会报错。原因是括号里面的东西是相应需要导的包,包导不对自然会报错咯。今天没有学到多少东西,在寻找路径和通过命令行对AndroID的shell *** 作上面耽搁了太多时间,不过还是很有收获,还跟网课的老师进行了讨论,这是从来没有过的经历。总而言之明天需要加快进度才行,争取明天顺顺利利的解决AndroID端存储的学习。

 

总结

以上是内存溢出为你收集整理的Android开发之数据持久化存储三全部内容,希望文章能够帮你解决Android开发之数据持久化存储三所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1065876.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存