SD Card 外部存储使用详解

和你一起终身学习,这里是程序员Android

经典好文推荐,通过阅读本文,您将收获以下知识点:

一、保存外部存储需要申请权限
二、外部存储使用案例(保存,读取,删除图片)

一、 保存外部存储需要申请权限

Android设备支持外部存储,比如SD卡等,保存在外部存储的数据具有全局可读性,可供在其他设备比如电脑上阅读,修改等。使用外部存储需要获取外部存储的访问权限<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

这个很重要,不然无法操作SD 卡,

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

二、外部存储使用案例(保存,读取,删除图片)

1. 实现效果

外部存储保存图片的方法

2. 判断是否挂载 SD 卡方法

    /**
     * 1.判断SD卡是否挂载
     * **/
    public static boolean isMounted() {

        String state = Environment.getExternalStorageState();
        return state.equals(Environment.MEDIA_MOUNTED);

    }

SD 保存图片,删除图片、显示图片的方法

3. 保存图片到SD卡

保存图片到SD卡 实现代码如下:

    // 保存图片的方法
    public void BtnSaveImage(View view) {
        // 获取图片类型 bitmap
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
                R.drawable.ic_launcher);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        // 将bitmap 压缩成byte类型 并保存到outputstream中
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
        bitmap.recycle();
        boolean saveimg = SaveImg(getApplicationContext(), "photo.png",
                baos.toByteArray());
        if (saveimg) {
            Toast.makeText(getApplicationContext(), "保存成功" + store_path,
                    Toast.LENGTH_SHORT).show();
        }
        try {
            baos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 保存图片的方法
    public static boolean SaveImg(Context context, String filename, byte[] data) {
        // 判断是否挂载SD卡
        if (!isMounted()) {
            Toast.makeText(context, "SD卡未安装", Toast.LENGTH_SHORT).show();
            return false;
        }
        File dir = new File(store_path);
        // 创建文件目录
        if (!dir.exists()) {
            dir.mkdirs();
        }
        try {
            // 向文件目录 dir中写文件filename
            FileOutputStream fos = new FileOutputStream(new File(dir, filename));
            fos.write(data);
            fos.close();
            return true;

        } catch (IOException e) {
            e.printStackTrace();
            Log.i("TAG", "IOException..." + e);
            return false;
        }
    }

4. 删除图片的方法

删除图片 代码实现代码实现如下:

    public void BtnDeleteImage(View view) {
        DeletleImg(getApplicationContext(), "photo.png");

    }

    // 删除图片
    public static void DeletleImg(Context context, String filename) {

        File dirfile = new File(store_path + filename);
        // 判断文件是否存在
        if (!dirfile.exists()) {
            Toast.makeText(context, "文件不存在", Toast.LENGTH_SHORT).show();
            return;
        }
        if (dirfile.isDirectory()) {
            String[] childdir = dirfile.list();
            for (int i = 0; i < childdir.length; i++) {
                new File(dirfile, childdir[i]).delete();
            }
        }
        dirfile.delete();
    }

5.读取显示图片的方法

读取显示图片代码实现如下:

// 读取图片
    public void BtnReadImage(View view) {
        Bitmap readImg = ReadImg(getApplicationContext(), "photo.png");
        if (readImg == null) {
            Toast.makeText(getApplicationContext(), "读取失败" + store_path,
                    Toast.LENGTH_SHORT).show();
        } else {
            ((ImageView) findViewById(R.id.img_external))
                    .setImageBitmap(readImg);
        }

    }

    // 读取图片
    public static Bitmap ReadImg(Context context, String filename) {
        // 判断是否挂载SD卡
        if (!isMounted()) {
            Toast.makeText(context, "SD卡未安装", Toast.LENGTH_SHORT).show();
            return null;
        }
        // 获取文件路径下的文件名称
        File imgFile = new File(store_path, filename);
        if (imgFile.exists()) {
            Log.i("TAG", "imgFile" + imgFile.getAbsolutePath());
            // 将路径下的文件转换成 bitmap
            return BitmapFactory.decodeFile(imgFile.getAbsolutePath());
        } else {
            Toast.makeText(context, "文件不存在", Toast.LENGTH_SHORT).show();
        }

        return null;
    }

6. 布局如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/img_external"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/btn_external_save"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="BtnSaveImage"
        android:text="保存图片到SD卡" />

    <Button
        android:id="@+id/btn_external_delete"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="BtnDeleteImage"
        android:text="删除SD卡 图片" />

    <Button
        android:id="@+id/btn_external_read"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="BtnReadImage"
        android:text="显示SD卡 图片" />

</LinearLayout>

至此,本篇已结束。转载网络的文章,小编觉得很优秀,欢迎点击阅读原文,支持原创作者,如有侵权,恳请联系小编删除。同时感谢您的阅读,期待您的关注。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员Android

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值