在Android中将文件路径转换为Uri [英] Convert a file path to Uri in Android

查看:3926
本文介绍了在Android中将文件路径转换为Uri的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,可以使用相机捕获视频.我可以获取视频的文件路径,但是我需要它作为Uri.

I have an app where I capture a video using the camera. I can get the video's file path, but I need it as a Uri.

我得到的文件路径:

/storage/emulated/0/DCIM/Camera/20141219_133139.mp4

我需要的是这样的

content//media/external/video/media/18576.

这是我的代码.

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // if the result is capturing Image

         if (requestCode == CAMERA_CAPTURE_VIDEO_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                // video successfully recorded
                // preview the recorded video
                // selectedImageUri = data.getData();
                // Uri selectedImage = data.getData();
                previewVideo();

                tv1.setText(String.valueOf((fileUri.getPath())));
                String bedroom=String.valueOf((fileUri.getPath()));
                Intent i = new Intent();
                i.putExtra(bhk1.BEDROOM2, bedroom);
                setResult(RESULT_OK,i); 
                btnRecordVideo.setText("ReTake Video");

            } else if (resultCode == RESULT_CANCELED) {
                // user cancelled recording
                Toast.makeText(getApplicationContext(),
                        "User cancelled video recording", Toast.LENGTH_SHORT)
                        .show();
            } else {
                // failed to record video
                Toast.makeText(getApplicationContext(),
                        "Sorry! Failed to record video", Toast.LENGTH_SHORT)
                        .show();
            }
        }
    }

我需要String变量bedroom中的Uri.

I need a Uri from the String variable bedroom.

推荐答案

请尝试以下代码

Uri.fromFile(new File("/sdcard/sample.jpg"))

这篇关于在Android中将文件路径转换为Uri的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆