2017-01-07 1 views
0

비디오 경로가 있습니다. 이 경로를 사용하여 비디오에서 비디오를로드 할 수 있습니다.파일 경로에서 비디오 정보 가져 오기

이제 db의 비디오 정보를 원하지만 내 Cursor은 항상 null입니다. 다음은 제 코드입니다.

String videoPath=Uri.fromFile(new File("/storage/emulated/0/Android/data/files/1483767006415.mp4") 
    final String[] projection = new String[]{MediaStore.Video.Media._ID, MediaStore.Video.Media.DISPLAY_NAME, MediaStore.Video.Media.DATA, 
      MediaStore.Video.Media.DURATION}; 
    CursorLoader loader = new CursorLoader(getActivity(), contentUri, projection, null, null, null); 
    Cursor cursor = loader.loadInBackground(); 
    if (cursor != null && cursor.moveToFirst()) { 
     long id = cursor.getLong(cursor.getColumnIndex(projection[0])); 
     String name = cursor.getString(cursor.getColumnIndex(projection[1])); 
     String path = cursor.getString(cursor.getColumnIndex(projection[2])); 
     long duration = cursor.getLong(cursor.getColumnIndex(projection[3])); 
     cursor.close(); 
     return new ImageObject(id, name, path, false, MEDIA_TYPE_VIDEO, duration); 
    } 

uri가 적절하지 않은 것으로 보입니다.

감사합니다.

+0

이렇게하면됩니다. 그것은 당신을 위해 일할 수도 있습니다 http://stackoverflow.com/questions/13613443/android-mediastore-video-query-is-returning-null – Gautam

+0

@ Gautam : nope. 작동하지 않습니다. 이 링크를 확인했습니다. – Beena

답변

0

마지막으로 답변을 얻었습니다. 내 URI에 문제가 있습니다. 동영상 ID가 URI에 추가되지 않았습니다. 그래서 나는 비디오 ID를 먼저 얻은 다음 createdURI를 얻었습니다. 이 URI를 사용하면 모든 정보를 얻을 수있었습니다. 아래는 동일한 코드입니다.

Uri mainUri; 
    Cursor cursor1 = getContentResolver().query(
      MediaStore.Video.Media.EXTERNAL_CONTENT_URI, 
      new String[]{MediaStore.Video.Media._ID}, 
      MediaStore.Video.Media.DATA + "=? ", 
      new String[]{pathMain}, null); 
    if (cursor1 != null && cursor1.moveToFirst()) { 
     int id = cursor1.getInt(cursor1.getColumnIndex(MediaStore.MediaColumns._ID)); 
     cursor1.close(); 
     mainUri = Uri.withAppendedPath(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, "" + id); 
    } else { 
     ContentValues values = new ContentValues(); 
     values.put(MediaStore.Video.Media.DATA, pathMain); 
     mainUri = getContentResolver().insert(
       MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values); 

    } 
    final String[] projection = new String[]{MediaStore.Video.Media._ID, MediaStore.Video.Media.DISPLAY_NAME, MediaStore.Video.Media.DATA, 
      MediaStore.Video.Media.DURATION}; 
    String selection = MediaStore.Video.Media.DATA + "=?"; 
    String selectionArgs[] = {pathMain}; 
    CursorLoader loader = new CursorLoader(getActivity(), mainUri, projection, selection, selectionArgs, null); 
    Cursor cursor = loader.loadInBackground(); 
    if (cursor != null && cursor.moveToFirst()) { 

     long id = cursor.getLong(cursor.getColumnIndex(projection[0])); 
     String name = cursor.getString(cursor.getColumnIndex(projection[1])); 
     String path = cursor.getString(cursor.getColumnIndex(projection[2])); 
     long duration = cursor.getLong(cursor.getColumnIndex(projection[3])); 
     cursor.close(); 
     return new ImageObject(id, name, path, false, MEDIA_TYPE_VIDEO, duration); 

    } 
관련 문제