3

get path of video file selected from gallery getting NULL. How to get path of video file ? Get URi in Activity Result also give null. converting Uri to String also getting 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();
        }
    }
}

This is my 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);
}

Selected Video from Gallery I hope its OK ? Check it

    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);
    }
}
Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95
Harsh Bhavsar
  • 1,561
  • 4
  • 21
  • 39
  • what version of android you used to test. – USKMobility Jun 11 '16 at 08:51
  • NeXus 4(5.1.1) API 22 – Harsh Bhavsar Jun 11 '16 at 08:55
  • There is no requirement for `ACTION_GET_CONTENT` to return to you a `Uri` that maps to a file that you can access. The user can choose a file on removable storage, or in the internal storage of some other app (e.g., Dropbox), or an encrypted file, etc. The sooner you stop thinking in terms of file paths, the more likely it is that you will have success. – CommonsWare Jun 11 '16 at 11:12

1 Answers1

12

Update your getPath method

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;
    }
USKMobility
  • 5,721
  • 2
  • 27
  • 34