I try to save an video to sd card. To test my codes, I save the selected video from gallery to another folder.
I call gallery intent, and pick a video. onActivityResult function code is below :
if(requestCode == VIDEO_PICK) {
if (resultCode == RESULT_OK) {
try {
String path = getRealPathFromURI(data.getData());
Log.i("@@Path ", " " + path);
File file = new File(new URI(path));
Uri uri = addVideo(file);
} catch(Exception err) {
Log.i("@@Exception", " " + err);
}
}
}
public Uri addVideo(File videoFile) {
ContentValues values = new ContentValues();
values.put(MediaStore.Video.Media.TITLE, "My video title");
values.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4");
values.put(MediaStore.Images.Media.DISPLAY_NAME, "player");
values.put(MediaStore.Images.Media.DESCRIPTION, "");
values.put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis());
values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());
values.put(MediaStore.Video.Media.DATA, videoFile.getAbsolutePath());
return getActivity().getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);
}
addVideo function throws an exception.
java.lang.IllegalArgumentException: URI is not absolute:
How can I get absolute path from uri? I tried uri.toString() and uri.getPath(). They didn't work.
getRealPathFromUri implementation is here