I have to develop an application in which I am downloading a video file from an URL After downloading, I am playing it through an Intent.
But every time I am getting the same message: "You can't play this video."
download code:
protected String doInBackground(String... params) {
File file = new File(getFilesDir(), generateFileName(params[0]));
try {
URL u = new URL(params[0]);
URLConnection conn = u.openConnection();
int contentLength = conn.getContentLength();
DataInputStream stream = new DataInputStream(u.openStream());
byte[] buffer = new byte[contentLength];
stream.readFully(buffer);
stream.close();
DataOutputStream fos = new DataOutputStream(new FileOutputStream(file));
fos.write(buffer);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
method for playing the video:
public void playVideo(String fileName) {
Uri data = Uri.parse(new File(fileName).getAbsolutePath());
Log.i("DATA: ",""+data);
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(data, "video/mp4");
startActivity(intent);
}