android createTempFile 抛出权限被拒绝?

发布于 2024-09-18 06:23:28 字数 2754 浏览 7 评论 0 原文

这很简单,但不起作用。我正在尝试创建一个临时文件(后来成为永久存储文件)用于预览 MP3 文件。我已尝试后缀的以下变体,如下例所示:

public class StudyFileIo extends Activity {
    private static final String TAG = "StudyFileIo";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        try {
            File tempFooFile = File
            .createTempFile("foo", "dat");
            Log.i(TAG, tempFooFile.getAbsolutePath());
        } catch (IOException e) {
            Log.e(TAG, e.toString());
            e.printStackTrace();
        }

    }
}

日志:

09-07 11:25:20.299 E/StudyFileIo( 8859): java.io.IOException: Permission denied
09-07 11:25:20.299 W/System.err( 8859): java.io.IOException: Permission denied
09-07 11:25:20.299 W/System.err( 8859):     at java.io.File.createNewFileImpl(Native Method)
09-07 11:25:20.299 W/System.err( 8859):     at java.io.File.createNewFile(File.java:1160)
09-07 11:25:20.299 W/System.err( 8859):     at java.io.File.createTempFile(File.java:1224)
09-07 11:25:20.299 W/System.err( 8859):     at java.io.File.createTempFile(File.java:1182)
09-07 11:25:20.299 W/System.err( 8859):     at com.mobibob.studyfileio.StudyFileIo.onCreate(StudyFileIo.java:25)
09-07 11:25:20.299 W/System.err( 8859):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-07 11:25:20.299 W/System.err( 8859):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
09-07 11:25:20.309 W/System.err( 8859):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-07 11:25:20.309 W/System.err( 8859):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-07 11:25:20.309 W/System.err( 8859):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-07 11:25:20.309 W/System.err( 8859):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-07 11:25:20.309 W/System.err( 8859):     at android.os.Looper.loop(Looper.java:123)
09-07 11:25:20.309 W/System.err( 8859):     at android.app.ActivityThread.main(ActivityThread.java:4627)
09-07 11:25:20.309 W/System.err( 8859):     at java.lang.reflect.Method.invokeNative(Native Method)
09-07 11:25:20.309 W/System.err( 8859):     at java.lang.reflect.Method.invoke(Method.java:521)
09-07 11:25:20.319 W/System.err( 8859):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:876)
09-07 11:25:20.319 W/System.err( 8859):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:634)
09-07 11:25:20.319 W/System.err( 8859):     at dalvik.system.NativeStart.main(Native Method)

是否缺少某些 AndroidManifest.xml 设置(我正在使用默认清单)?

This is simple, but not working. I am trying to create a temp file (later a permanent storage file) for preview of an MP3 file. I have tried the following variants of the suffix as following example:

public class StudyFileIo extends Activity {
    private static final String TAG = "StudyFileIo";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        try {
            File tempFooFile = File
            .createTempFile("foo", "dat");
            Log.i(TAG, tempFooFile.getAbsolutePath());
        } catch (IOException e) {
            Log.e(TAG, e.toString());
            e.printStackTrace();
        }

    }
}

Log:

09-07 11:25:20.299 E/StudyFileIo( 8859): java.io.IOException: Permission denied
09-07 11:25:20.299 W/System.err( 8859): java.io.IOException: Permission denied
09-07 11:25:20.299 W/System.err( 8859):     at java.io.File.createNewFileImpl(Native Method)
09-07 11:25:20.299 W/System.err( 8859):     at java.io.File.createNewFile(File.java:1160)
09-07 11:25:20.299 W/System.err( 8859):     at java.io.File.createTempFile(File.java:1224)
09-07 11:25:20.299 W/System.err( 8859):     at java.io.File.createTempFile(File.java:1182)
09-07 11:25:20.299 W/System.err( 8859):     at com.mobibob.studyfileio.StudyFileIo.onCreate(StudyFileIo.java:25)
09-07 11:25:20.299 W/System.err( 8859):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-07 11:25:20.299 W/System.err( 8859):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
09-07 11:25:20.309 W/System.err( 8859):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-07 11:25:20.309 W/System.err( 8859):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-07 11:25:20.309 W/System.err( 8859):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-07 11:25:20.309 W/System.err( 8859):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-07 11:25:20.309 W/System.err( 8859):     at android.os.Looper.loop(Looper.java:123)
09-07 11:25:20.309 W/System.err( 8859):     at android.app.ActivityThread.main(ActivityThread.java:4627)
09-07 11:25:20.309 W/System.err( 8859):     at java.lang.reflect.Method.invokeNative(Native Method)
09-07 11:25:20.309 W/System.err( 8859):     at java.lang.reflect.Method.invoke(Method.java:521)
09-07 11:25:20.319 W/System.err( 8859):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:876)
09-07 11:25:20.319 W/System.err( 8859):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:634)
09-07 11:25:20.319 W/System.err( 8859):     at dalvik.system.NativeStart.main(Native Method)

Is there some AndroidManifest.xml setting that I am missing (I am using default manifest)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

难忘№最初的完美 2024-09-25 06:23:28

您需要在应用程序拥有的目录中创建临时文件。您应该使用 createTempFile(String prefix, String suffix, File directory),其中目录是临时文件所在的位置书面。您可以通过 目录的有效位置“noreferrer”>Context.getFilesDir()Context.getDir(String name, int mode)

You need to create the temp files in a directory that your application owns. You should use createTempFile(String prefix, String suffix, File directory), where directory is the location to which the temp file is to be written. You can get a valid location for directory with the result from Context.getFilesDir() or Context.getDir(String name, int mode).

九公里浅绿 2024-09-25 06:23:28

我认为您只是错过了在外部存储中写入的权限,因为默认情况下会在外部存储中创建临时文件。
添加

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

到您的清单中,它应该可以工作。

I think that you just missed the permission to write at the external storage, since temp files are created there by default.
Add

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

to your manifest and it should work.

南风几经秋 2024-09-25 06:23:28

即使添加后
这表明我遇到了错误。
但是添加这个之后
android:requestLegacyExternalStorage="true"
在清单文件的应用程序标签中,我能够创建TempFile并能够从手机捕获图片。

<应用程序 android:requestLegacyExternalStorage="true"

Even after adding
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" this to manifest I was getting error.
But after adding this
android:requestLegacyExternalStorage="true"
in application tag of manifest file I am able to createTempFile and able capture pic from phone.

<application android:requestLegacyExternalStorage="true"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文