我按照tutorial进行拍照并保存到设备存储中,但是当我执行它时,它会出现异常
{ "dependencies": { "Microsoft.NETCore.App": { "version": "1.0.1", "type": "platform" }, "Microsoft.AspNetCore.Diagnostics": "1.0.0", "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0", "Microsoft.AspNetCore.Server.Kestrel": "1.0.1", "Microsoft.Extensions.Logging.Console": "1.0.0", "Microsoft.AspNetCore.StaticFiles": "1.0.0" }, "tools": { "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final" }, "frameworks": { "netcoreapp1.0": { "imports": [ "dotnet5.6", "portable-net45+win8" ] } }, "buildOptions": { "emitEntryPoint": true, "preserveCompilationContext": true }, "runtimeOptions": { "configProperties": { "System.GC.Server": true } }, "publishOptions": { "include": [ "wwwroot", "web.config" ] }, "scripts": { "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ] } }
如何解决这个问题?
java.io.IOException: Permission denied
on line:
File image = File.createTempFile(imageFileName, ".jpg", storageDir);
- 编辑 -
这是我的清单
private File createImageFile() throws IOException
{
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "Camera");
File image = File.createTempFile(imageFileName, ".jpg", storageDir);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
return image;
}
答案 0 :(得分:4)
与Android Documentation一样,您需要写入外部存储空间,您必须在WRITE_EXTERNAL_STORAGE中申请manifest file权限:
如果您使用API 23(Marshmallow)及以上版本,则需要Requesting Permissions at Run Time,因为它是Dangerous Permission。
答案 1 :(得分:1)
在清单文件中添加权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
答案 2 :(得分:1)
将权限放在清单文件中:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
并在try catch块中添加createTempFile代码:
File storageDir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File file = null;
try {
file = File.createTempFile(timeStamp, ".jpg", storageDir);
} catch (IOException e) {
e.printStackTrace();
}
mCurrentPhotoPath = String.valueOf(Uri.fromFile(file));
return file;
答案 3 :(得分:1)
如果您的目标 api 级别 >=29 或者您使用的是 android 10,那么请在应用程序标签的应用清单文件中添加以下行
android:requestLegacyExternalStorage="true"
答案 4 :(得分:-1)
将此标记写入项目的清单文件中,并在运行之前通过Android手机中的设置授予您的应用权限。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>