How to save mp4 video in android

     final InputStream input = (InputStream) reponse;
                String filename = "/ad" + ".mp4";
                String root = Environment.getExternalStorageDirectory().toString();

                File myDir = new File(root + File.separator + "xxx");
                myDir.mkdirs();
                File file = new File(myDir.getAbsolutePath(), filename);

                try {

                    OutputStream output = new FileOutputStream(file);
                    try {
                        try {
                            byte[] buffer = new byte[4 * 1024]; // or other buffer size
                            int read;

                            while ((read = input.read(buffer)) != -1) {
                                output.write(buffer, 0, read);
                            }
                            output.flush();
                        } finally {
                            output.close();
                        }
                    } catch (Exception e) {
                        e.printStackTrace(); // handle exception, define IOException and others
                    }
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
                String path=file.getPath().toString();
                if (path != null) {
                    final Message message = new Message();
                    message.what = VIDEOBUFFER;
                    message.obj = path;
                    handler.sendMessage(message);
                }

Guess you like

Origin blog.csdn.net/Ser_Bad/article/details/50947644