2

갤러리에서 선택한 비디오 파일의 경로를 가져 오는 중 NULL입니다. 비디오 파일의 경로를 얻는 방법? 활동에서 URi 받기 결과 또한 null입니다. Uri을 문자열로 변환하면 Null이됩니다.갤러리에서 선택한 비디오 파일의 경로가 NULL이됩니다. 비디오 파일의 경로를 얻는 방법?

Intent intent; 
String selectedVideo1Path, selectedVideo2Path; 
EditText e1,e2; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.videos_activity); 
    Button button1 = (Button) findViewById(R.id.video1_btn); 
    Button button2 = (Button) findViewById(R.id.video2_btn); 



    button1.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      selectVideoFromGallery(); 
      startActivityForResult(intent, 101); 
     } 
    }); 

    button2.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      selectVideoFromGallery(); 
      startActivityForResult(intent, 102); 
     } 
    }); 



} 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == 101) { 
     if (data.getData() != null) { 
      selectedVideo1Path = getPath(data.getData()); 

      Toast.makeText(MergeVideosActivity.this, "Path 1 : "+selectedVideo1Path, Toast.LENGTH_SHORT).show(); 


     } else { 
      Toast.makeText(getApplicationContext(), "Failed to select video", Toast.LENGTH_LONG).show(); 
     } 
    } 
    if (requestCode == 102) { 
     if (data.getData() != null) { 
      selectedVideo2Path = getPath(data.getData()); 
      //String str2 = selectedVideo2Path.toString(); 
      // e2.setText(str2); 
      Toast.makeText(MergeVideosActivity.this, "Path 2 : "+selectedVideo2Path, Toast.LENGTH_SHORT).show(); 

     } else { 
      Toast.makeText(getApplicationContext(), "Failed to select video", Toast.LENGTH_LONG).show(); 
     } 
    } 
} 

getPath method 갤러리에서 비디오 선정

public String getPath(Uri uri) { 
    int column_index = 0; 
    String[] projection = {MediaStore.Images.Media.DATA}; 
    Cursor cursor = managedQuery(uri, projection, null, null, null); 
    if (cursor != null) { 
     column_index = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA); 
     cursor.moveToFirst(); 

    } 
    return cursor.getString(column_index); 
} 

나는 그것의 확인을 희망 내이있다? 이

public void selectVideoFromGallery() { 
     if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) { 
      intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI); 
     } else { 
      intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Video.Media.INTERNAL_CONTENT_URI); 
     } 
     intent.setType("video/*"); 
     intent.setAction(Intent.ACTION_GET_CONTENT); 
    } 
} 
+0

확인합니다. – USKMobility

+0

NeXus 4 (5.1.1) API 22 –

+0

'ACTION_GET_CONTENT'가 액세스 할 수있는 파일에 매핑되는'Uri'를 반환 할 필요는 없습니다. 사용자는 이동식 저장소 나 다른 응용 프로그램 (예 : Dropbox)의 내부 저장소 또는 암호화 된 파일에서 파일을 선택할 수 있습니다. 파일 경로의 관점에서 생각할수록 파일을 더 빨리 찾을 수있게 될 가능성이 높습니다. 성공할 것이다. – CommonsWare

답변

9

업데이트를 테스트하는 데 사용되는 안드로이드의 버전을 당신의 getPath 방법

public String generatePath(Uri uri,Context context) { 
     String filePath = null; 
     final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT; 
     if(isKitKat){ 
      filePath = generateFromKitkat(uri,context); 
     } 

     if(filePath != null){ 
      return filePath; 
     } 

     Cursor cursor = context.getContentResolver().query(uri, new String[] { MediaStore.MediaColumns.DATA }, null, null, null); 

     if (cursor != null) { 
      if (cursor.moveToFirst()) { 
       int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA); 
       filePath = cursor.getString(columnIndex); 
      } 
      cursor.close(); 
     } 
     return filePath == null ? uri.getPath() : filePath; 
    } 

    @TargetApi(19) 
    private String generateFromKitkat(Uri uri,Context context){ 
     String filePath = null; 
     if(DocumentsContract.isDocumentUri(context, uri)){ 
      String wholeID = DocumentsContract.getDocumentId(uri); 

      String id = wholeID.split(":")[1]; 

      String[] column = { Media.DATA }; 
      String sel = Media._ID + "=?"; 

      Cursor cursor = context.getContentResolver(). 
        query(Media.EXTERNAL_CONTENT_URI, 
          column, sel, new String[]{ id }, null); 



      int columnIndex = cursor.getColumnIndex(column[0]); 

      if (cursor.moveToFirst()) { 
       filePath = cursor.getString(columnIndex); 
      } 

      cursor.close(); 
     } 
     return filePath; 
    } 
+0

에 의해 발생 : java.lang.NullPointerException : null 객체 참조에 가상 메소드 'android.content.pm.PackageManager android.content.Context.getPackageManager()'를 호출하려고 시도했습니다. –

+0

yourActivity.this와 같은 활동 인스턴스로 컨텍스트를 바꿉니다.이 – USKMobility

+2

이것은 정답이어야합니다. 고맙습니다. – airowe