Harsh Bhavsar
Harsh Bhavsar

Reputation: 1671

get path of video file selected from gallery getting NULL. How to get path of video file?

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);
    }
}

Upvotes: 3

Views: 6817

Answers (1)

Umesh Singh Kushwaha
Umesh Singh Kushwaha

Reputation: 5741

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;
    }

Upvotes: 12

Related Questions