Android 安卓应用程序可以';无法将文件保存到sd卡?
我正在尝试将文件从服务器下载到外部sd卡。这是我的密码:Android 安卓应用程序可以';无法将文件保存到sd卡?,android,permissions,android-external-storage,Android,Permissions,Android External Storage,我正在尝试将文件从服务器下载到外部sd卡。这是我的密码: try { URL url = new URL(address); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod("GET"); urlConnection.setDoOutp
try {
URL url = new URL(address);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.connect();
File SDCardRoot = Environment.getExternalStorageDirectory();
File file = new File(SDCardRoot,"myfile.txt");
FileOutputStream fileOutput = new FileOutputStream(file);
InputStream inputStream = urlConnection.getInputStream();
//this is the total size of the file
int totalSize = urlConnection.getContentLength();
int downloadedSize = 0;
byte[] buffer = new byte[1024];
int bufferLength = 0; //used to store a temporary size of the buffer
while ( (bufferLength = inputStream.read(buffer)) > 0 )
fileOutput.write(buffer, 0, bufferLength);
downloadedSize += bufferLength;
publishProgress(downloadedSize, totalSize);
}
fileOutput.close();
问题是,应用程序似乎无法找到外部sd卡位置:
03-20 13:03:29.615 16992-17244/com.example.myapp W/System.err: java.io.FileNotFoundException: /storage/emulated/0/myfile.txt: open failed: EACCES (Permission denied)
是否需要添加一些新的权限?我已添加读写外部存储权限。有什么我遗漏的吗?非常感谢您的任何意见 以下是我的AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
“我已经添加了读写外部存储权限”--请发布您的清单。另外,您的
targetSdkVersion
是什么版本,您测试的安卓是什么版本?“到外部sd卡”--不是。@commonware谢谢,我已经在上面的清单中编辑过了。我的targetSdkVersion是23,我也在23上测试。您受到运行时权限的影响,很可能是: