【问题标题】:Is this code outdated? getExternalStoragePublicDirectory()这段代码过时了吗?获取外部存储公共目录()
【发布时间】:2019-10-03 03:44:55
【问题描述】:

为什么这段代码是我画白线的问题 什么问题 有什么办法可以代替吗?

File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC);

正是这行代码用白线法画出来的 getExternalStoragePublicDirectory

【问题讨论】:

    标签: android path filepath


    【解决方案1】:

    getExternalStoragePublicDirectory() --> 已在 API 级别 29 中弃用

    为了改善用户隐私,直接访问共享/外部存储 设备已弃用。当应用程序以 Build.VERSION_CODES.Q 为目标时, 从此方法返回的路径不再可直接访问 应用。应用程序可以继续访问存储在共享/外部的内容 通过迁移到替代方案来存储,例如 Context#getExternalFilesDir(String)、MediaStore 或 意图#ACTION_OPEN_DOCUMENT。

    void createExternalStoragePrivateFile() {
        // Create a path where we will place our private file on external
        // storage.
        File file = new File(getExternalFilesDir(null), "DemoFile.jpg");
    
        try {
            // Very simple code to copy a picture from the application's
            // resource into the external file.  Note that this code does
            // no error checking, and assumes the picture is small (does not
            // try to copy it in chunks).  Note that if external storage is
            // not currently mounted this will silently fail.
            InputStream is = getResources().openRawResource(R.drawable.balloons);
            OutputStream os = new FileOutputStream(file);
            byte[] data = new byte[is.available()];
            is.read(data);
            os.write(data);
            is.close();
            os.close();
        } catch (IOException e) {
            // Unable to create file, likely because external storage is
            // not currently mounted.
            Log.w("ExternalStorage", "Error writing " + file, e);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-25
      • 2013-03-08
      • 2020-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多