繁体   English   中英

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

[英]Problem in creating Temporary File in android?

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

File temp = File.createTempFile("mediaplayertmp", "dat");

在运行它时抛出异常

Parent directory of file in not 
writable:/sdcard/
mediaplayertmp43912.dat

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

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

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

我不确定你想要做什么......如果目录不可写,那么它是不可写的。 为用户告诉他们他们的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
}

我有同样的问题。 我的应用程序工作正常,直到我更新SDK。 现在需要WRITE_EXTERNAL_STORAGE权限才能写入SD卡。

该代码在Android 1.5或更早版本中按原样运行。 更新的任何内容都要求应用程序在其清单中明确要求WRITE_EXTERNAL_STORAGE权限。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM