0

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

Community
  • 1
  • 1

1 Answers1

0

Should I create a file from uri?

If the Uri scheme is file, then getPath() will be a filesystem path pointing to the file, and presumably you can read it.

If the Uri scheme is content, then getPath() is a meaningless string. Ideally, you just use the Uri itself, or you use ContentResolver and openInputStream() to get an InputStream on the content identified by the Uri. If, for some reason, you absolutely need a file, use that InputStream to copy the bytes of the content to some file that you control.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491