Im downloading files from a server, some big, some small.
protected Long doInBackground(Object... params) {
Context context = (Context) params[0];
String name = (String) params[1];
String urlString = (String) params[2];
File mediadir = context.getDir("tvr", Context.MODE_PRIVATE);
try {
URL url = new URL(urlString);
URLConnection connection = url.openConnection();
connection.connect();
int lenghtOfFile = connection.getContentLength();
InputStream is = url.openStream();
Log.d("DOWNLOAD NAME",name);
File new_file = new File(mediadir, name);
FileOutputStream fos = new FileOutputStream(new_file.getPath());
byte data[] = new byte[1024];
int count = 0;
long total = 0;
int progress = 0;
while ((count=is.read(data)) != -1){
total += count;
int progress_temp = (int)total*100/lenghtOfFile;
if(progress_temp%10 == 0 && progress != progress_temp){
progress = progress_temp;
}
fos.write(data, 0, count);
}
is.close();
fos.close();
} catch (Exception e) {
// TODO Auto-generated catch block
Log.e("DOWNLOAD ERROR", e.toString());
}
return null;
}
As I now understand it, Im downloading the file via a stream and streaming the data into a file. So in effect the file is created since the first byte of data.
Now, on the other end of things Im playing files from my internal directory by looking for files in a directory and then playing them:
mediaPlayer = new MediaPlayer();
mediaPlayer.setOnCompletionListener(this);
mediaPlayer.setDisplay(holder);
FileInputStream fileInputStream = new FileInputStream(path);
mediaPlayer.setDataSource(fileInputStream.getFD());
fileInputStream.close();
mediaPlayer.prepare();
mediaPlayer.start();
Now this issue is that the file will be played even though its not fully downloaded!
Is this true? If so, how must I check if the file is complete before trying to play it?