Ich entwickle eine App, in der ich PDF-Dateien aus dem Internet nur in SD-Karte herunterladen wollte.öffnen fehlgeschlagen: EACCES (Berechtigung verweigert)
Ich verwende android kitkat (4.4.4)
. Ich habe Fehler beim Senden der Datei java.io.FileNotFoundException: /storage/sdcard1/pdf/953.pdf: open failed: EACCES (Permission denied)
. Und wenn ich android (4.2)
verwende, funktioniert es gut.
wenn ich diesen Code Environment.getExternalStorageDirectory();
in android 4.4.4
ausführen, gibt es mir internen Speicher zurück. wo wie in android 4.2
gibt es mir SD-Karte Speicherort.
Hier ist mein Code.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ashishkudale.filedownload">
<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"/>
<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">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
MainActivity.java
public class MainActivity extends AppCompatActivity {
private Button bt_Download;
private ProgressDialog progressDialog;
public static final int progress_bar_type = 0;
private static String file_url = "http://my_website_url/Data/953.pdf";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt_Download = (Button) findViewById(R.id.bt_Download);
bt_Download.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new DownloadFile().execute(file_url);
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case progress_bar_type:
progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Downloading...");
progressDialog.setMax(100);
progressDialog.setCancelable(true);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setIndeterminate(false);
progressDialog.show();
return progressDialog;
default:
return null;
}
}
class DownloadFile extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(progress_bar_type);
}
@Override
protected String doInBackground(String... fileUrl) {
int count;
try {
URL url = new URL(fileUrl[0]);
URLConnection conection = url.openConnection();
conection.connect();
int lenghtOfFile = conection.getContentLength();
InputStream input = new BufferedInputStream(url.openStream(), 8192);
File file = new File("/storage/sdcard1/pdf","953.pdf");
OutputStream output = new FileOutputStream(file);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress(""+(int)((total*100)/lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return null;
}
protected void onProgressUpdate(String... progress) {
progressDialog.setProgress(Integer.parseInt(progress[0]));
}
@Override
protected void onPostExecute(String fileUrl) {
dismissDialog(progress_bar_type);
}
}
}
Es in Ausnahme gefangen wird. Hier ist meine Ausnahmebedingung. java.io.FileNotFoundException: /storage/sdcard1/pdf/953.pdf: open failed: EACCES (Permission denied)
Ich weiß nicht, warum android 4.4.4
und höher mir nicht erlaubt, in SD-Karte zu schreiben.
Ich habe auch so viel im Internet gesucht, konnte aber keine konkrete Antwort bekommen. Gibt es eine andere Möglichkeit, dies zu tun? lass es mich wissen, bitte.
kann ich Datei mit Datei/Ordner-Picker schreiben. –
@AshishKudale: Das ist, was 'ACTION_CREATE_DOCUMENT' tut. – CommonsWare
danke. Jetzt kann ich eine Dateiauswahl sehen. aber beim Speichern des Dokuments zeigt es mir Toast "konnte nicht speichern Dokument" –