4

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); 
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
hharry_tech
  • 952
  • 1
  • 8
  • 24
  • Try to download this video via browser and play it. If it will not work, it looks like the view has no compability format. If it will work, post your readFully method – Alexander Mikhaylov May 15 '14 at 08:19
  • Is your path to file is correct? – Abhishek May 15 '14 at 08:33
  • did you succeed? And which android version do you have? I have a similar problem. Works fine on Android 4.2 (Samsung Galaxy S3) but not on 4.4.4 (Nexus 4) – romedius Sep 09 '14 at 13:02

1 Answers1

0

This code worked for me when supplying extra infomation to an intent. It's not video data, but image data, but I believe the concept should be the same (I could be wrong, since I didn't try this on video).

File file = new File(getExternalFilesDir(null), "image.png");
String uriPath = "file://"+file.getPath(); 
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(uriPath));

Note that the image I am supplying to the intent is downloaded before this of course and then saved in the directory of the app (this I get with getExternalFilesDir(null)). Then the image is passed to an intent as a stream.

If this does not help you then maybe check if there are any apps installed that can handle your type of video (is it mp4 or what is the format?)

zaifrun
  • 931
  • 9
  • 21