【问题标题】:Exception in saving file on Android Marshmallow在 Android Marshmallow 上保存文件时出现异常
【发布时间】:2016-01-20 00:44:13
【问题描述】:

之前,我在我的根设备上使用KitKat,我能够轻松地创建临时文件。但是现在我在我的 Nexus 5 上安装了Marshmallow,同样的代码给了我一个例外。

相关代码为:

File storageDir = Environment.getExternalStoragePublicDirectory(
        Environment.DIRECTORY_DOWNLOADS);

//      File repertoireStockage = Environment.getExternalStorageDirectory();
        try {
            file1 = File.createTempFile("RecordAudio",".mp4", storageDir);
        } 
        catch (IOException e) {
            Log.e("RecordAudio", "I/O Problem");
            e.printStackTrace();
            return;
        }

是的,我确实拥有Manifest.xml 文件中application 标记之外的权限

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

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

之前,我使用注释行来获取存储目录,但现在,两者都不起作用。任何形式的帮助将不胜感激。

我试过拔掉数据线后保存文件,还是一样的异常。

【问题讨论】:

  • 请分享你的logcat!
  • 您必须请求权限。 Marshamallow 具有“运行时权限”。阅读此幻灯片以了解最新动态goo.gl/photos/9R6ss39caQHLyEmU6

标签: android android-sdcard android-permissions


【解决方案1】:

该操作系统带来了许多新功能,它还具有一个内置的文件管理器,许多用户可能不会注意到。

许多第三方文件管理器允许用户重命名文件、创建新文件夹、剪切文件等。但是,Android 6.0 Marshmallow 的板载文件管理器不允许此类操作。

请注意,它们返回的文件路径会在 该应用程序在内部和外部存储设备之间移动。什么时候 构建文件路径,强烈建议您始终调用 这些 API 是动态的。不要使用硬编码的文件路径或持久化 之前构建的完全限定的文件路径。

参考链接

1) http://developer.android.com/about/versions/marshmallow/android-6.0.html

2) http://www.techtimes.com/articles/94044/20151012/android-6-0-marshmallow-has-a-built-in-file-manager-heres-how-to-access-it.htm

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-01
    • 1970-01-01
    • 2013-03-13
    • 2019-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-08
    相关资源
    最近更新 更多