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

本小妞迷上赌 提交于 2019-12-24 02:14:05

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/37761797/get-path-of-video-file-selected-from-gallery-getting-null-how-to-get-path-of-vi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!