我试图从照片库中选择一个图像文件并写入SD卡.以下是导致异常的代码.在尝试创建FileOutputStream时,它似乎抛出此异常.我将以下行添加到嵌套在application元素中的清单文件中.我找不到问题的解决方案:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
public boolean saveSelectedImage( Uri selectedImage, int imageGroup,
int imageNumber )
{
boolean exception = false;
InputStream input = null;
OutputStream output = null;
if( externalStorageIsWritable() )
{
try
{
ContentResolver content = ctx.getContentResolver();
input = content.openInputStream( selectedImage );
if(input != null) Log.v( CLASS_NAME, "Input Stream Opened successfully");
File outFile = null;
File root = Environment.getExternalStorageDirectory( );
if(root == null) Log.v(CLASS_NAME, "FAILED TO RETRIEVE DIRECTORY");
else Log.v(CLASS_NAME, "ROOT DIRECTORY is:"+root.toString());
output = new FileOutputStream( root+"/Image"+ imageGroup + …Run Code Online (Sandbox Code Playgroud) 正如你可以从标题中注意到的那样,我在Android中将文件写入sdcard时遇到了问题.我已经检查了这个问题,但它没有帮助我.我想写一个将在sdcard上的公共空间中的文件,以便任何其他应用程序都可以读取它.
首先,我检查是否安装了SD卡:
Environment.getExternalStorageState();
Run Code Online (Sandbox Code Playgroud)
然后,我运行此代码:
File baseDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
baseDir.mkdirs();
File file = new File(baseDir, "file.txt");
try {
FileOutputStream out = new FileOutputStream(file);
out.flush();
out.close();
Log.d("NEWFILE", file.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
我有:
<manifest>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application>
...
</application>
</manifest>
Run Code Online (Sandbox Code Playgroud)
在我的AndroidManifest.xml中.
确切的错误是这样的:
java.io.FileNotFoundException: /storage/1510-2908/Download/secondFile.txt: open failed: EACCES (Permission denied)
Run Code Online (Sandbox Code Playgroud)
我正在模拟器上测试我的代码(模拟Nexus5 API 23).我所需的最低SDK版本是19(4.4 Kitkat).
此外,一切正常,使用相同的代码将文件写入SD卡上的私人文件夹,所以我说前代码也应该工作:
File newFile = new File(getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "esrxdtcfvzguhbjnk.txt");
newFile.getParentFile().mkdirs();
try {
FileOutputStream out = new FileOutputStream(newFile);
out.flush();
out.close();
Log.d("NEWFILE", newFile.getAbsolutePath());
} catch (IOException e) …Run Code Online (Sandbox Code Playgroud) 我需要将一些数据保存到SD卡,我已经将权限添加到AndroidManifest.xml文件中,当我在Android 4.12 mobile中测试它时,我可以得到正确的结果.
但是当我在Android 5.1移动设备中测试它时,我打开失败:EACCES(Permission denied)错误,为什么?
顺便说一句,我已经阅读了Android 6.0开放失败:EACCES(Permission denied)和Exception'开放失败:EACCES(Permission denied)'在Android上,但现在我的手机是SamSung Android 5.1
码
private void ActionUploadFiles(Map<String, String> files,IHTTPSession session,String uploadFolder){
try{
Set<String> keys = files.keySet();
for (String key:keys) {
String location = files.get(key);
File source = new File(location);
String filename= session.getParms().get(key);
filename=FilenameUtils.getName(filename);
File target = new File(uploadFolder,filename);
FileUtils.copyFile(source,target);
}
}
catch (Exception e) {
Utility.LogError("Upload Error: "+ e.getMessage());
}
}
Run Code Online (Sandbox Code Playgroud)
AndroidManifest.xml中
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="info.dodata.wifi">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" …Run Code Online (Sandbox Code Playgroud) 当我尝试将位图存储到存储中时出现此错误 #
File path = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "picture");
if (! path.exists()) {
path.mkdirs();
if (!path.exists()) {
return null;
}
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HH_mm_ss", Locale.CHINA).format(new Date());
File imagePath = new File(path.getPath() + "_" + "IMG_" + timeStamp + ".jpg");
BufferedOutputStream fos;
try {
fos =new BufferedOutputStream(new FileOutputStream(imagePath));
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
return imagePath;
} catch (FileNotFoundException e) {
Log.e("GREC", e.getMessage(), e);
return null;
} catch (IOException e) {
Log.e("GREC", e.getMessage(), e);
return null;
}
Run Code Online (Sandbox Code Playgroud)
fos = new BufferedOutputStream(new …