Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.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
Java 如何将字节数组转换为视频文件并在视频视图中设置_Java_Android_File_File Handling - Fatal编程技术网

Java 如何将字节数组转换为视频文件并在视频视图中设置

Java 如何将字节数组转换为视频文件并在视频视图中设置,java,android,file,file-handling,Java,Android,File,File Handling,我正在尝试使用下面的代码对视频文件进行编码 byte[] decodedBytes = Base64.decode(video, Base64.DEFAULT); InputStream input = new ByteArrayInputStream(decodedBytes); OutputStream output = null; try { File file = new File (Environment.getExternalStorageDirectory().

我正在尝试使用下面的代码对视频文件进行编码

 byte[] decodedBytes =  Base64.decode(video, Base64.DEFAULT);
 InputStream input = new ByteArrayInputStream(decodedBytes);
 OutputStream output = null;
 try {
     File file = new File (Environment.getExternalStorageDirectory().toString(),"video.mp4");
     output  = new FileOutputStream(file);
         
     byte[] data = new byte[decodedBytes.length];
     int count;
     while ((count = input.read(data)) != -1) {
         output.write(data, 0, count);
     }
     output.close();
  
     Uri video = Uri.parse(file.getAbsolutePath());
     videoView.setVideoURI(video);
     videoView.start();
 } catch (IOException e) {
     Log.e("check123", e.toString());
     e.printStackTrace();
 }
但我在运行时收到以下错误消息:

应用程序无法播放此视频

我找到了解决办法

public class VideoDemo extends Activity {

private MediaController ctlr;


VideoView videoView = null;

Context context = null;
long totalRead = 0;
int bytesToRead = 50 * 1024;

private int mPlayerPosition;
private File mBufferFile;

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    getWindow().setFormat(PixelFormat.TRANSLUCENT);
    setContentView(R.layout.main);




    videoView = (VideoView) findViewById(R.id.videoview);


    ctlr = new MediaController(this);

    ctlr.setMediaPlayer(videoView);
    videoView.setMediaController(ctlr);
    videoView.requestFocus();

    new GetYoutubeFile().start();


}



private class GetYoutubeFile extends Thread {
    private String mUrl;
    private String mFile;

    public GetYoutubeFile() {

    }

    @Override
    public void run() {
        super.run();
        try {

            File bufferingDir = new File(
                    Environment.getExternalStorageDirectory()
                            + "/YoutubeBuff");
            InputStream stream = getAssets().open("famous.3gp");
            if (stream == null)
                throw new RuntimeException("stream is null");
            File temp = File.createTempFile("test", "mp4");
            System.out.println("hi");
            temp.deleteOnExit();
            String tempPath = temp.getAbsolutePath();

            File bufferFile = File.createTempFile("test", "mp4");

            BufferedOutputStream bufferOS = new BufferedOutputStream(
                    new FileOutputStream(bufferFile));


            InputStream is = getAssets().open("famous.3gp");
            BufferedInputStream bis = new BufferedInputStream(is, 2048);

            byte[] buffer = new byte[16384];
            int numRead;
            boolean started = false;
            while ((numRead = bis.read(buffer)) != -1) {

                bufferOS.write(buffer, 0, numRead);
                bufferOS.flush();
                totalRead += numRead;
                if (totalRead > 120000 && !started) {
                    Log.e("Player", "BufferHIT:StartPlay");
                    setSourceAndStartPlay(bufferFile);
                    started = true;
                }

            }
            mBufferFile = bufferFile;

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

public void setSourceAndStartPlay(File bufferFile) {
    try {

        mPlayerPosition=videoView.getCurrentPosition();
        videoView.setVideoPath(bufferFile.getAbsolutePath());

        videoView.start();

    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public void onCompletion(MediaPlayer mp) {
    mPlayerPosition = mp.getCurrentPosition();
    try {
        mp.reset();
        videoView.setVideoPath(new File("mnt/sdcard/YoutubeBuff/"
                + mBufferFile).getAbsolutePath());
        mp.seekTo(mPlayerPosition);
        videoView.start();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }

}
在我的xml中,我只有视频视图

byte[]data=新字节[decodedBytes.length];整数计数;while((count=input.read(data))!=-1{output.write(data,0,count);}
。替换为
output.write(解码字节)
。你能给我建议另一种方法来解码编码的base64视频字符串并在android studio的videoview上设置它吗?这是我正在使用的编码代码:videoView.buildDrawingCache();位图位图=videoView.getDrawingCache();java.io.ByteArrayOutputStream=新java.io.ByteArrayOutputStream();compress(bitmap.CompressFormat.WEBP,90,stream);字节[]videoByteArray=stream.toByteArray();video=Base64.encodeToString(videoByteArray,0);另一种方式?你试过我的密码吗?你为什么不报告?是的,我试过你的密码。