Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将字节数组另存为.mp4文件并在VideoView android中播放_Android_Video_Android Videoview - Fatal编程技术网

将字节数组另存为.mp4文件并在VideoView android中播放

将字节数组另存为.mp4文件并在VideoView android中播放,android,video,android-videoview,Android,Video,Android Videoview,我使用自定义视频活动捕获了一个视频,并使用此方法将其编码为base64 private String encodeVideoTobase64(Uri uri , int index) { String videodata = ""; String[] filePathColumn = { MediaStore.Video.Media.DATA }; Cursor cursor = this.getContentResolver().query(uri,fileP

我使用自定义视频活动捕获了一个视频,并使用此方法将其编码为base64

private String encodeVideoTobase64(Uri uri , int index)
{
    String videodata     = "";
    String[] filePathColumn = { MediaStore.Video.Media.DATA };

    Cursor cursor = this.getContentResolver().query(uri,filePathColumn, null, null, null);
    String videoPath;
    try{
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        videoPath       = cursor.getString(columnIndex);
    }
    catch(Exception e)
    {
        videoPath = PathsOfVideos.get(index);
    }
    try {
        @SuppressWarnings("resource")
        FileInputStream v_input = new FileInputStream(videoPath);
        ByteArrayOutputStream objByteArrayOS = new ByteArrayOutputStream();
        byte[] byteBufferString = new byte[1024];
        for (int readNum; (readNum = v_input.read(byteBufferString)) != -1;) 
        {
            objByteArrayOS.write(byteBufferString, 0, readNum);
            System.out.println("read " + readNum + " bytes,");
        }

       videodata = MyBase64.encodeBytes(byteBufferString);//Base64.encodeToString(objByteArrayOS.toByteArray(), Base64.DEFAULT);

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return videodata;
}
然后,我将编码后的视频发送到服务器

现在我想获取视频base64字符串,解码它并在视频视图中显示它。 我曾尝试将base64字符串转换为字节数组,然后将其保存到手机,然后使用此代码将其显示在视频视图中

protected void showInstVideo(int pos) {
    // TODO Auto-generated method stub
    final Dialog dialog = new Dialog(this);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    dialog.setContentView(R.layout.dialog_video);

    //Decode String To Video With mig Base64.
    String encodedString = r.getRecipeInstructions().get(pos).getVideoStr();
    if (encodedString.compareTo("")!=0) {
        byte[] decodedBytes = MiGBase64.decodeFast(encodedString.getBytes());

        try {
            Date date=new Date();
            String filename="/rec"+ date.toString().replace(" ", "_").replace(":", "_")+".mp4";
            String root = Environment.getExternalStorageDirectory().toString();
            File myDir  = new File(root+"/MyAppName");
            if (!myDir.exists())
                myDir.mkdir();
            File file = new File (myDir, filename);

            FileOutputStream out = new FileOutputStream(file);
            out.write(decodedBytes);
            out.close();

            VideoView instvideo     = (VideoView) dialog.findViewById(R.id.vvdetails);
            MediaController mediaController = new MediaController(this);
            mediaController.setAnchorView(instvideo);
            Uri video = Uri.parse(file.getPath());
            instvideo.setVideoURI(video);

            if (video != null )
            {
                dialog.show();
                instvideo.start();
            }
        } catch (Exception e) {
            // TODO: handle exception
            Log.e("Error", e.toString()); 
        }
    }


}
但是文件夹/MyAppName未创建,因此视频未保存,因此我无法播放视频

你能帮我找到我丢失的东西吗? 提前谢谢

注意:代码运行正确,不会捕获任何异常,而不是
mkdir()
。您也可以使用
mkdirs()
mkdirs()
将创建所有目录,直到给定路径中的最后一个目录

替换此代码:

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

File myDir  = new File(root+"/MyAppName");
if (!myDir.exists())
    myDir.mkdir();
File file = new File (myDir, filename);

是否已添加权限是此权限存在。但奇怪的是,我发现所有创建的文件夹突然出现。我不知道是什么原因使他们不能立即出现。。你知道吗?是的..文件操作需要更多的时间..你需要使用AsynchTask来完成..并在onPostExecute完成后进行检查哦,我的上帝!!这样,当用户希望即时播放视频时,我无法播放视频:O.有没有办法在不将视频保存到设备的情况下播放bytearray视频?我认为在android中不可能。你需要保存视频,然后只播放。我发现所有创建的文件夹突然出现。我不知道是什么原因使他们不能立即出现。。你知道吗?