What for? I cannot play the downloaded mp4 video using Intent

I need to develop an application in which I download a video file from a URL. After downloading, I play it through intent.

But every time I get the same message: "You cannot 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;
 }

video playback method:

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); 
}
+4
source share
1 answer

This code worked for me while providing additional information for the intent. This is not video data, but image data, but I believe that the concept should be the same (I could be wrong, since I did not try it on video).

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

, , , , ( getExternalFilesDir (null)). .

, , , - , ( mp4 ?)

0

Source: https://habr.com/ru/post/1540652/


All Articles