我有个网址。它是视频的直接链接。如何将其转换为文件?
我找到了一个如何转换图像的解决方案
Bitmap bitmap = null;
InputStream input = new URL(imgUrl).openStream();
bitmap = BitmapFactory.decodeStream(input);
File file = new File(getCacheDir(), "1");
file.createNewFile();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
byte[] bitmapdata = bos.toByteArray();
FileOutputStream fos = new FileOutputStream(file);
fos.write(bitmapdata);
fos.flush();
fos.close();
但我找不到如何转换视频?
要将直接视频链接转换为文件,您需要实现下面的代码。
InputStream input = new URL(videoUrl).openStream();
File file = new File(getCacheDir(), "1");
try (OutputStream outputStream = new FileOutputStream(file)) {
IOUtils.copy(input, outputStream);
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
并且需要为IOUtils
类连接此依赖项实现“commons-io:commons-io:2.6”
。快乐编码。