Android 6:无法访问错误文件
安卓6的问题让我发疯了。从服务器下载pdf文件(在localhost中运行。服务器响应为HTTP 200(因此未发现问题),但当我尝试打开文件时,出现错误:“错误文件无法访问” 这是下载文件的Asynctack:Android 6:无法访问错误文件,android,android-6.0-marshmallow,file-not-found,Android,Android 6.0 Marshmallow,File Not Found,安卓6的问题让我发疯了。从服务器下载pdf文件(在localhost中运行。服务器响应为HTTP 200(因此未发现问题),但当我尝试打开文件时,出现错误:“错误文件无法访问” 这是下载文件的Asynctack: private class DownloadContent extends AsyncTask<String,Integer,String> { private Context context; private PowerManager.WakeLock m
private class DownloadContent extends AsyncTask<String,Integer,String> {
private Context context;
private PowerManager.WakeLock mWakeLock;
private String ris;
public DownloadContent(Context context) {
this.context = context;
}
@Override
protected String doInBackground(String... params) {
String nomefile=params[0];
String email=params[1];
String id=params[2];
InputStream input = null;
String[] saveFiles;
OutputStream output = null;
HttpURLConnection connection = null;
try {
URL url = new URL("http://192.168.1.8:8080/com.appcampus/rest/content/"+nomefile+"/"+email+"/"+id);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
return "Server returned HTTP " + connection.getResponseCode()
+ " " + connection.getResponseMessage();
}
int fileLength = connection.getContentLength();
// download the file
java.io.File xmlFile = new java.io.File(Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
+ "/"+nomefile);
input = connection.getInputStream();
int permissionCheck = ContextCompat.checkSelfPermission(getApplicationContext(),
Manifest.permission.WRITE_EXTERNAL_STORAGE);
if(permissionCheck == PackageManager.PERMISSION_GRANTED){
output = new FileOutputStream(xmlFile);
byte data[] = new byte[4096];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
// allow canceling with back button
if (isCancelled()) {
input.close();
return null;
}
total += count;
// publishing the progress....
if (fileLength > 0) // only if total length is known
publishProgress((int) (total * 100 / fileLength));
output.write(data, 0, count);
}
}else{
Log.d("Permission check",""+permissionCheck);
}
saveFiles = context.fileList();
String s = "";
for (String element : saveFiles) {
s += " " + element;
}
ris = "Server returned HTTP " + connection.getResponseCode();
} catch (Exception e) {
return e.toString();
} finally {
try {
if (output != null)
output.close();
if (input != null)
input.close();
} catch (IOException ignored) {
}
if (connection != null)
connection.disconnect();
}
return ris;
}
}
我认为清单上的所有许可都可以:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<!-- To retrieve the account name (email) as part of sign-in: -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<!-- To auto-complete the email text field in the login form with the user's emails -->
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
最终我找到了解决方案:我在要打开的文件路径中添加了file://
Uri path = Uri.parse("file:///"+Environment
.getExternalStorageDirectory().toString()
+ "/YOURFILE.pdf");
原始文件有多少字节?下载的ond.Figures有多少字节!
risultato=downloadTask.execute(param).get();
.Bad code。不要使用.get()方法。而是在onPostExecute()中处理doInBackground()的结果.Act normal。我尝试在onPostExecute中处理结果,但没有任何更改。请求返回HTTP 200。我尝试使用文件资源管理器查找该文件,但在我的设备上找不到。这怎么可能?我尝试检查(file.exists())是否返回false。但安卓棒棒糖的相同代码是可以的。
Uri path = Uri.parse("file:///"+Environment
.getExternalStorageDirectory().toString()
+ "/YOURFILE.pdf");