【发布时间】:2020-08-08 08:40:15
【问题描述】:
我正在尝试使用相机意图上传视频以解析服务器以捕获视频。 代码如下。现在我的问题是视频正在使用 videoUri 在 videoView 中播放,但它没有上传到服务器。我收到一个 FileNotFoundException 说“不存在这样的文件或目录”, 例子:- I/info: content://media/external/video/media/57463 //这是videoUri的日志输出// W/System.err:java.io.FileNotFoundException:/external/video/media/57463(没有这样的文件或目录) 有人可以帮忙解决这个问题吗?
public class VideoActivity extends AppCompatActivity {
static final int REQUEST_VIDEO_CAPTURE = 1;
Uri videoUri,vUri;
ParseUser currentUser;
VideoView videoView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
currentUser=ParseUser.getCurrentUser();
Button captureVideo = (Button) findViewById(R.id.captureVideo);
videoView=(VideoView) findViewById(R.id.videoView);
Button saveButton=(Button) findViewById(R.id.saveButton);
//save Button clicks handled here.
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(videoUri!=null){
Log.i("info", ""+videoUri);
byte[] bytes = convertVideoToBytes(videoUri);
//now lets try and add this uri file to the parse server in a parsefile.
ParseFile parseVideoFile = new ParseFile("video.mp4", bytes);
parseVideoFile.saveInBackground();
currentUser.put("video", parseVideoFile);
currentUser.saveInBackground();
}else{
Toast.makeText(VideoActivity.this,"No Video to save",Toast.LENGTH_LONG).show();
}
}
});
//capture video button click is handled here.
captureVideo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
if (takeVideoIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takeVideoIntent, REQUEST_VIDEO_CAPTURE);
}
}
});
}
//onActivityResult is shown here.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (requestCode == REQUEST_VIDEO_CAPTURE && resultCode == RESULT_OK) {
videoUri = intent.getData();
videoView.setVideoURI(videoUri);
videoView.start();
}
}
//this is to convert the videoUri to byte[] arrays.
public static byte[] convertVideoToBytes( Uri videoUri) {
byte[] videoBytes = null;
File inputFile=new File(videoUri.getPath());
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
FileInputStream fis = new FileInputStream(inputFile);
byte[] buf = new byte[(int)inputFile.length()];
int n;
while (-1 != (n = fis.read(buf)))
baos.write(buf, 0, n);
videoBytes = baos.toByteArray();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return videoBytes;
}
}
【问题讨论】:
-
content://media/external/video/media/57463这是一个不错的内容方案。一个 uri。 -
/external/video/media/57463这是内容方案的最后一部分。它不是文件系统路径,因此不能在其上使用 File 和 FileOutputSteam 类。 -
不是在 uri 的一部分上打开 FileInputStream,而是为 uri 本身打开 InputStream。然后以与 FileInputStream 相同的方式从 InputStream 中读取。
-
InputStream is = getContentResolver().openInputStream(uri); -
非常感谢,代码就像一个魅力。我能够将视频上传到解析服务器,但现在我必须尝试从服务器下载它并在我的应用程序中播放视频..如果你也有解决方案吗?可以分享一下吗?
标签: android android-studio parse-platform video-capture android-camera-intent