如何将视频路径更改为视频Uri

时间:2016-10-12 13:54:07

标签: android

在我的应用中,我使用自定义相机录制视频。我可以进行录制并从SD卡获取视频路径,如下所示:<div class="image pull-left">IMAGE</div> <table class="pull-left"> <tr> <td>Column 1</td> <td>Column 2</td> </tr> <tr> <td>Column 1</td> <td>Column 2</td> </tr> </table> <div class="clearfix"></div> <table class="pull-left"> <tr> <td>Column 1</td> <td>Column 2</td> </tr> <tr> <td>Column 1</td> <td>Column 2</td> </tr> </table> <table class="pull-left"> <tr> <td>Column 1</td> <td>Column 2</td> </tr> <tr> <td>Column 1</td> <td>Column 2</td> </tr> </table>

现在我想在youtube上传这个视频,但是要上传视频,我需要URI中的路径,如下所示: /storage/emulated/0/DCIM/Camera/20141219_133139.mp4

请告诉我如何将路径转换为URI。 在此先感谢。

2 个答案:

答案 0 :(得分:0)

  content://media/external/video/media/18576

表示视频在媒体数据库中的id = 18576。您必须在媒体数据库中查询id。伪代码:

   select id from 'content//media/external/video/media' 
   where data = '/storage/emulated/0/DCIM/Camera/20141219_133139.mp4'

你必须查询媒体数据库

// getIdForFile("/storage/emulated/0/DCIM/Camera/20141219_133139.mp4")
public long getIdForFile(String filePath) {
    Cursor c = null;
    try {
        c = this.getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
                new String[]{MediaStore.Video.Media._ID}, 
                MediaStore.Video.Media.DATA + " = ?",
                new String[]{filePath},
                null              // Default sort order
        );
        if (c.moveToNext()) {
            return c.getInt(0); // MediaStore.Video.Media._ID
        }
    } catch (Exception ex) {
        Log.e(Global.LOG_CONTEXT, ".... some error message ....", ex);
    } finally {
        if (c != null) c.close();
    }
    return null;
}

答案 1 :(得分:0)

它的工作!

 long vidid = getIdForFile(mediaFile.getAbsolutePath());
 Log.d("VIDID" , " "+vidid);
 String urivid =  "content://media/external/video/media/"+vidid;
 Uri vid = Uri.parse(urivid); //  it's right uri  for videofile

我的函数getIdForFile

public long getIdForFile(String filePath) {
    Cursor c = null;
    try {
        c = this.getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
                new String[]{MediaStore.Video.Media._ID},
                MediaStore.Video.Media.DATA + " = ?",
                new String[]{filePath},
                null              // Default sort order
        );
        if (c.moveToNext()) {
            return c.getInt(0); // MediaStore.Video.Media._ID
        }
    } catch (Exception ex) {
        Log.e("VIDURI", ".... some error message ....", ex);
    } finally {
        if (c != null) c.close();
    }
    return 0;
}