Android 无法在外部存储器上写入文件
我想在外部存储器上写入文件,但出现错误:Android 无法在外部存储器上写入文件,android,storage,android-file,Android,Storage,Android File,我想在外部存储器上写入文件,但出现错误: java.io.FileNotFoundException:/mnt/media\u rw/sdcard1/Backup/Contact/18-03-2016 16:03:17.vcf:open失败:EACCES(权限被拒绝) 我的代码: fileContact = new File("/mnt/media_rw/sdcard1/", "Backup/Contact"); if (!fileCon
java.io.FileNotFoundException:/mnt/media\u rw/sdcard1/Backup/Contact/18-03-2016 16:03:17.vcf:open失败:EACCES(权限被拒绝)
我的代码:
fileContact = new File("/mnt/media_rw/sdcard1/", "Backup/Contact");
if (!fileContact.exists()) {
fileContact.mkdirs();
}
Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:MM:ss");
String strDate = sdf.format(c.getTime());
StoragePath = fileContact.getAbsolutePath() + "/" + strDate + ".vcf";
当我写入fileContact=new文件(“/sdcard/”,“/Backup/Contact”)
然后将文件写入内部存储器
。我想将此文件写入我的存储卡(外部SD卡)
我正在从此代码获取所有已装入/外部存储列表-http://stackoverflow.com/a/19982338/3774906
它为我提供了完美的列表
清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest
package="com.xxx.xx"
xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-feature android:name="android.hardware.usb.host"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:autoRemoveFromRecents="true"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
那么,我如何将此文件写入外部存储卡?您下面的访问外部存储的行似乎有问题
fileContact = new File("/mnt/media_rw/sdcard1/", "Backup/Contact");
某些设备制造商不使用sdcard1
访问外部文件时,请始终遵循此方法
请显示您的完整清单…@Opiatefuchs添加了我的完整清单。请不要硬编码路径。Environment.getExternalStorageDirectory().getAbsolutePath()将为您提供SD卡的路径。但请注意,一些制造商甚至会向您提供virtuell sd卡。阅读docs:but Environment.getExternalStorageDirectory().getAbsolutePath()上的路径,它只提供内部存储路径。您的硬编码路径不在我遇到的任何设备上。对于某些设备,可能是这样。您没有对Android 4.4+上的可移动存储的任意访问权限。状态将为我提供外部存储状态?我已经阅读了这篇文章,并且我也安装了状态。甚至我也写了
fileContact=new文件(getExternalFilesDir(null),“MagicSpy/Backup/Contact”)代码>但它不会在已安装的外部sd卡上创建文件
String state = Environment.getExternalStorageState();
File root = android.os.Environment.getExternalStorageDirectory();