110/12 - 把照片储存在Pictures/应用程序名称资料夹 - 2

Android 11开始把getExternalStoragePublicDirectory标记弃用,要求改用MediaStore,一样从画面开始

<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".CropLensActivity">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.google.android.material.button.MaterialButton
            android:id="@+id/aclMbCreatePackageNamePicture"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="4dp"
            android:layout_marginEnd="8dp"
            android:padding="8dp"
            android:text="在应用程序资料夹建立照片"
            android:textAllCaps="false"
            app:autoSizeTextType="uniform"
            app:layout_constraintBottom_toTopOf="@+id/aclIvPackageNamePicture"
            app:layout_constraintDimensionRatio="5:1"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_chainStyle="packed" />

        <androidx.appcompat.widget.AppCompatImageView
            android:id="@+id/aclIvPackageNamePicture"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_margin="4dp"
            android:layout_marginTop="4dp"
            app:layout_constraintBottom_toTopOf="@+id/aclMbCreatePhonePicture"
            app:layout_constraintDimensionRatio="4:3"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/aclMbCreatePackageNamePicture"
            tools:src="@tools:sample/backgrounds/scenic" />

        <com.google.android.material.button.MaterialButton
            android:id="@+id/aclMbCreatePhonePicture"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="4dp"
            android:layout_marginEnd="8dp"
            android:padding="8dp"
            android:text="在图片资料夹建立照片"
            android:textAllCaps="false"
            app:autoSizeTextType="uniform"
            app:layout_constraintBottom_toTopOf="@+id/aclIvPhonePicture"
            app:layout_constraintDimensionRatio="5:1"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/aclIvPackageNamePicture" />

        <androidx.appcompat.widget.AppCompatImageView
            android:id="@+id/aclIvPhonePicture"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_margin="4dp"
            android:layout_marginTop="4dp"
            app:layout_constraintDimensionRatio="4:3"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/aclMbCreatePhonePicture"
            tools:src="@tools:sample/backgrounds/scenic" />

        <com.google.android.material.button.MaterialButton
            android:id="@+id/aclMbCreateMediaStorePicture"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="4dp"
            android:layout_marginEnd="8dp"
            android:padding="8dp"
            android:text="MediaStore在图片资料夹建立照片"
            android:textAllCaps="false"
            app:autoSizeTextType="uniform"
            app:layout_constraintDimensionRatio="5:1"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/aclIvPhonePicture" />

        <androidx.appcompat.widget.AppCompatImageView
            android:id="@+id/aclIvMediaStorePicture"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_margin="4dp"
            android:layout_marginTop="4dp"
            app:layout_constraintDimensionRatio="4:3"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/aclMbCreateMediaStorePicture"
            tools:src="@tools:sample/backgrounds/scenic" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>

以前使用getExternalStoragePublicDirectory我们还要自己建立资料夹,使用MediaStore後直接填入路径,ContentValues就会自动帮我们建立资料夹,非常方便。

不过用这种方式储存档案,如果遇到同名档案,会在档案後面加上(数字),例如原本有003.jpg,下次存档就会变成003(1).jpg,以此类推。

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_crop_lens)

    aclMbCreateMediaStorePicture.setOnClickListener {
    
        val contentValue = ContentValues().apply {
            this.put(MediaStore.Images.ImageColumns.DISPLAY_NAME, "003.jpg")
            this.put(MediaStore.Images.ImageColumns.MIME_TYPE, "image/jpeg")
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                this.put(MediaStore.Images.ImageColumns.RELATIVE_PATH, "${Environment.DIRECTORY_PICTURES}/AndroidSystem")
            }
        }
        
        val uri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValue)
        
        takePictureResultLauncher.launch(uri)
    }
}

<<:  [Day27] 基础的 Directive

>>:  day27 : k8s backup/restore/migrate with velero(下)

表格元件共用攻略

前面讲完表单这样常见又复杂的制作方式,其实在对付的是一种「以物件为根的」资料结构。并且同时又处理掉了...

[Day24] iT邦帮忙502 Bad Gateway怎麽办? 教你自动侦测网页修复了没!

昨天下午一点多我打开电脑准备发Day23的铁人赛文章时, 发现iT邦帮忙的网站出现502 Bad G...

【Vim 编辑器 配置指南】订制个人的编辑神器

欲善其事 先利其器 目录 前言 Vim 配置 Vim 插件 终端机指令 前言 工程师要学习 Vim ...

Day 19羊肉空心菜炒面

Finky总是大力的支持我的铁人菜,前几天提到他虽然挑食,但是喜欢空心菜,所以今天就决定用空心菜简单...

当TrustView(档案加密软件) 白老鼠的惨痛过程记录 ~

外部稽核要求文件需要有保护的机制 , 所以我们引进了 ~ 加密了哪些软件产生的文件 ? Micros...