Download klappt nicht auf verschiedenen Geräten

  • Antworten:3
Gerd Schluckebier
  • Forum-Beiträge: 20

10.10.2014, 10:43:16 via Website

Habe folgendes Problem:
Mit dem Code:
public String DownloadFile(String urlstring){
try{
URL url = new URL(urlstring);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET";);
urlConnection.setDoOutput(true);
urlConnection.connect();
File file = new File("sdcard/","Test2.txt";);
FileOutputStream fileOutput = new FileOutputStream(file);
InputStream inputStream = urlConnection.getInputStream();
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;
int progress=(int)(downloadedSize*100/totalSize);
}
fileOutput.close();
String st=new String(buffer);
return st;
} catch (MalformedURLException e) {
return e.toString();
}
catch (IOException e) {
return e.toString();
}
}

gelingt der Download eines Textfiles und die Abspeicherung sowie die Ausgabe in einem Textedit
auf meinem Samsung Galaxy ACE, aber nicht auf meinem Samsung Galaxy S3 mini. Es erfolgt immer die Meldung:
"Programm angehalten"
Habe noch keine Lösung für das Problem gefunden.
MfG

Antworten
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

10.10.2014, 13:21:13 via App

Ich habe die Befürchtubg dass eine NetworkOnMainException ausgelöst wird. Du kannst ab Android 3.x den Downloadcode nichtmehr im Mainthread ausführen. Dafür gibt es den AsyncTask.

LG Pascal //It's not a bug, it's a feature. :) ;)

Antworten
Fabian Simon
  • Forum-Beiträge: 359

10.10.2014, 13:22:41 via Website

oder , ab einer gewissen Android version kannst du nicht mehr überall auf der Platte speichern

Antworten
Gerd Schluckebier
  • Forum-Beiträge: 20

10.10.2014, 17:34:19 via Website

danke ; ich werd's umprogrammieren

Antworten