我正在尝试在我的android cordova插件中编写一个文件。这实际上很好用,但当我的客户端将插件插入应用程序的其余部分时,它就无法再写入文件了。错误是" java.io.IOException:Permission denied"在写文件时。
我在清单中指定了WRITE_EXTERNAL_STORAGE和READ_EXTERNAL_STORAGE,并编写了代码以在运行时请求权限。它们弹出,我点击接受。但由于某种原因,它仍然无法写出文件,并且上面提到了权限被拒绝错误。
有谁知道我能做什么?我无法解决为什么会发生这种情况,尤其奇怪的是a)它对我来说很好,并且b)它可以用来为客户工作,直到2周前。
制作文件的代码(非常标准,对我来说很好并保存到/storage/emulated/0/proto/2018_01_12_17_15_26_Image.jpg,只有在客户端集成到主cordova应用程序时才停止工作)
// Create the file directory
File myDir = new File(Environment.getExternalStorageDirectory() , "proto");
myDir.mkdirs();
debug("does the dir exist @ "+myDir.getAbsolutePath());
debug("myDir exists? "+myDir.exists());
if (!myDir.exists())
{
Tools.toast("failed to create dir "+myDir.getAbsolutePath(),c);
}
// create the file in the directory
File file = new File(myDir, fname);
try {
debug("creating file."+file.getAbsolutePath());
boolean fileCreated = file.createNewFile();
debug("fileCreated?"+fileCreated);
if (fileCreated) {
debug("write bitmap to "+file.getAbsolutePath());
// write the bitmap to that file
FileOutputStream outputStream = new FileOutputStream(file);
//maybe faster to use jpeg?
bitmap.compress(Bitmap.CompressFormat.JPEG, 70, outputStream);
//bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
outputStream.close();
debug("FILE WAS CREATED");
//put in gallery on phone
MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "My title" , "My Description");
}
else
{
Tools.toast("Failed! File was not created.",c);
}
} catch (IOException ex) {
debug("SAVE FAILED could not save file!!! "+ex.getMessage());
//Tools.toast("Could not save file: "+ex.getMessage(),c);
ex.printStackTrace();
}