我使用自定义视频活动捕获了视频,并使用此方法将其编码为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,因此视频没有保存,当然因此我无法播放视频。
你可以帮我找一下我错过的东西吗? 提前致谢注意:代码运行正常,不会捕获任何异常
答案 0 :(得分:1)
您也可以使用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);