数据存储——四种存储方式——存储卡的文件操作——私有存储空间与公共存储空间
作者:互联网
==============================================================================
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="5dp" > <TextView android:id="@+id/tv_path" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="@color/black" android:textSize="17sp" /> </LinearLayout>
package com.example.myapplication; import android.os.Build; import android.os.Environment; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.TextView; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView tv_path = findViewById(R.id.tv_path); // Android7.0之后默认禁止访问公共存储目录 // 获取系统的公共存储路径 String publicPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString(); // 获取当前App的私有存储路径 String privatePath = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).toString(); boolean isLegacy = true; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { // Android10的存储空间默认采取分区方式,此处判断是传统方式还是分区方式 isLegacy = Environment.isExternalStorageLegacy(); } String desc = "系统的公共存储路径位于" + publicPath + "\n\n当前App的私有存储路径位于" + privatePath + "\n\nAndroid7.0之后默认禁止访问公共存储目录" + "\n\n当前App的存储空间采取" + (isLegacy?"传统方式":"分区方式"); tv_path.setText(desc); } }
小米手机:
荣耀手机:
=============================================================================
<!-- 存储卡读写 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<!-- Android10新增权限MANAGE_EXTERNAL_STORAGE -->
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
标签:存储,存储卡,Environment,Build,import,android,存储空间 来源: https://www.cnblogs.com/xiaobaibailongma/p/16463645.html