开发者社区> 问答> 正文

使用File对象在Android(API> 24)中捕获和保存视频?

我正在编写要在其中录制视频的代码,然后在录制5秒钟后将其保存。我有以下代码在API <24上正常工作,但是对于API> 24,我得到了错误。

码:

public void startRecording() { File mediaFile = new File(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) + "/myvideo.mp4");

Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT,5);
fileUri = Uri.fromFile(mediaFile);

intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(intent, VIDEO_CAPTURE);

}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if (requestCode == VIDEO_CAPTURE) {
    if (resultCode == RESULT_OK) {
        Toast.makeText(this, "Video has been saved to:\n" +
                data.getData(), Toast.LENGTH_LONG).show();
    } else if (resultCode == RESULT_CANCELED) {
        Toast.makeText(this, "Video recording cancelled.",
                Toast.LENGTH_LONG).show();
    } else {
        Toast.makeText(this, "Failed to record video",
                Toast.LENGTH_LONG).show();
    }
}

} 错误:

2019-10-08 01:15:43.483 21573-21573/com.mobilecomputing.learn2sign E/AndroidRuntime: FATAL EXCEPTION: main Process: com.mobilecomputing.learn2sign, PID: 21573 android.os.FileUriExposedException: file:///storage/emulated/0/myvideo.mp4 exposed beyond app through ClipData.Item.getUri() at android.os.StrictMode.onFileUriExposed(StrictMode.java:1978) at android.net.Uri.checkFileUriExposed(Uri.java:2371) at android.content.ClipData.prepareToLeaveProcess(ClipData.java:963) at android.content.Intent.prepareToLeaveProcess(Intent.java:10228) at android.content.Intent.prepareToLeaveProcess(Intent.java:10213) at android.app.Instrumentation.execStartActivity(Instrumentation.java:1854) at android.app.Activity.startActivityForResult(Activity.java:4599) at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:676) at android.app.Activity.startActivityForResult(Activity.java:4557) at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:663) at com.mobilecomputing.learn2sign.PlayHelpVideo.startRecording(PlayHelpVideo.java:125) at com.mobilecomputing.learn2sign.PlayHelpVideo$1.onClick(PlayHelpVideo.java:46) at android.view.View.performClick(View.java:6669) at android.view.View.performClickInternal(View.java:6638) at android.view.View.access$3100(View.java:789) at android.view.View$PerformClick.run(View.java:26145) at android.os.Handler.handleCallback(Handler.java:873) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6898) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 我检查了一下,该错误是由于文件对象的语法引起的,并且在API> 24上不受支持。

尽管我可以找到其他有效的代码,但我对此代码是否经过细微调整感到好奇,这也使其适用于API> 24。也许在同一行。

谁可以帮我这个事 ?

展开
收起
被纵养的懒猫 2019-10-08 17:26:49 860 0
1 条回答
写回答
取消 提交回答
  • 权限问题,参考文章:http://gelitenight.github.io/android/2017/01/29/solve-FileUriExposedException-caused-by-file-uri-with-FileProvider.html

    2019-11-25 16:21:02
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载