问题
I programatically open camera to take a video. I tell camera to put the video file to a specified place using code like below:
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
File out = new File("/sdcard/camera.mp4");
Uri uri = Uri.fromFile(out);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(intent, GlobalUtility.CAMERA_VIDEO);
It works well on a HTC phone. But on my moto defy, it just ignore the MediaStore.EXTRA_OUTPUT parameter, and put the video to the default place. So then I use this code in onActivityResult() function to solve the problem:
private String getRealPathFromURI(Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
String realPath;
try {
File file = new File("/sdcard/camera.mp4");
if (!file.exists()) {
Uri videoUri = data.getData();
realPath = getRealPathFromURI(videoUri);
}
} catch (Exception ex) {
Uri videoUri = data.getData();
realPath = getRealPathFromURI(videoUri);
}
Hope this will help some others.
回答1:
Just because /sdcard/
is the sdcard directory on one phone and one build of Android doesn't mean that will stay consistent.
You will want to use Environment.getExternalStorageDirectory() as Frankenstein's comment suggests. This will always work to get the directory of the SD Card.
You will also want to check that the SD Card is currently mountable as the phone may be in USB Storage mode.
Try something like...
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
Log.d(TAG, "No SDCARD");
} else {
File out = new File(Environment.getExternalStorageDirectory()+File.separator+"camera.mp4");
}
回答2:
I have done this way and still didn't found any error..so please try this in you "moto defy" so I can know the reality.
To Call Intent :
Intent intent = new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
startActivityForResult(intent,2323);
In Activity on Result:
Uri contentUri = data.getData();
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String tmppath = cursor.getString(column_index);
videoView.setVideoPath(path);
来源:https://stackoverflow.com/questions/8060534/motorola-android-2-2-camera-ignore-extra-output-parameter