我最近更新到了 Android 4.3,每当我从应用程序中使用 Intent 启动它时,库存摄像机就开始表现得有点奇怪。
起初它只是崩溃并说“图库停止响应”。过了一会儿,我能够录制视频,但是单击“完成”会向我的应用程序返回一个空 URI,这导致它崩溃!
因此,我开始使用相同的代码测试 2.3.4 设备。视频应用程序返回了我可以在该设备上使用的正确 URI。在我获得 4.3 之前,相同的代码运行得非常好(有 4.2.2 库存 Galaxy Nexus)
这是一个从 4.3 版本的相机应用程序获取 null URI 的活动,但在 4.2.2 及更低版本的设备上运行良好。
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button pick = (Button) findViewById(R.id.button1);
pick.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
startActivityForResult(takeVideoIntent, 123);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == RESULT_OK){
if(requestCode == 123){
VideoView videoView = (VideoView) findViewById(R.id.videoView1);
videoView.setVideoURI(data.getData());
Log.d("Video", "URI "+data.getData());
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}
我该怎么做才能避免这种情况发生?这是否意味着这与不同制造商设备上的其他相机应用程序的工作方式不同?
我通过将视频意图更改为以下内容来解决此问题:
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
String fName = "VideoFileName.mp4";
File f = new File(fName);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
startActivityForResult(intent, CAMERA_VIDEO_REQUEST);
在活动结果中我得到的视频文件路径如下:
File f = new File(Environment.getExternalStorageDirectory().toString());
for (File temp : f.listFiles()) {
if (temp.getName().equals("VideoFileName.mp4")) {
f = temp;
break;
}
}
//f is the video file...
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)