android createTempFile 抛出权限被拒绝?
这很简单,但不起作用。我正在尝试创建一个临时文件(后来成为永久存储文件)用于预览 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 设置(我正在使用默认清单)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要在应用程序拥有的目录中创建临时文件。您应该使用
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 fromContext.getFilesDir()
orContext.getDir(String name, int mode)
.我认为您只是错过了在外部存储中写入的权限,因为默认情况下会在外部存储中创建临时文件。
添加
到您的清单中,它应该可以工作。
I think that you just missed the permission to write at the external storage, since temp files are created there by default.
Add
to your manifest and it should work.
即使添加后
这表明我遇到了错误。
但是添加这个之后
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"