在android中创建临时文件的问题?

Raj*_*ian 2 android

在我用于流视频的MediaPlayer应用程序中,我使用以下代码

File temp = File.createTempFile("mediaplayertmp", "dat");
Run Code Online (Sandbox Code Playgroud)

在运行它时抛出异常

Parent directory of file in not 
writable:/sdcard/
mediaplayertmp43912.dat
Run Code Online (Sandbox Code Playgroud)

我不知道如何处理这个问题,我想知道当我们执行该代码时意味着文件将被创建.任何人都知道解决方案意味着请帮助一些代码.

小智 17

您的请求是否允许在Android清单中写入SD卡?

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Run Code Online (Sandbox Code Playgroud)


Jer*_*gan 5

我不确定你想要做什么......如果目录不可写,那么它是不可写的.为用户告诉他们他们的SDCard需要写入权限(可能有关于如何修复的说明)的错误.

在几个应用程序中,我有类似这样的代码,以确保有一个SDCard ...不应该很难修改它以确保它也是可写的:

// make sure we have a mounted SDCard
if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
    // they don't have an SDCard, give them an error message and quit
    final AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage(R.string.welcome_dialog_sdcard_error)
        .setCancelable(false)
        .setPositiveButton(R.string.welcome_dialog_sdcard_ok, new DialogInterface.OnClickListener() {
            public void onClick(final DialogInterface dialog, final int id) {
                finish();
            }
        });
    final AlertDialog alert = builder.create();
    alert.show();
} else {
    // there's an SDCard available, continue
}
Run Code Online (Sandbox Code Playgroud)