简体   繁体   中英

Can't play video in videoview android studio?

I know that there is too many solutions were given, but I can't get the exact solution. My problem is that I have picked one video from internal storage device and after picking video then I have converted to String and set the video to videoView but then also it shows that "Can't play this video" in videoView.

can anyone please help me to find out the solution:(

here is my code

      File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Download/videos.mp4");
    Log.d("video",""+file);
    if (file.exists()) {
        Uri uri = Uri.fromFile(file);
        String video = String.valueOf(uri);
        Log.d("video",""+uri);
        videoView.setMediaController(new MediaController(this));
        videoView.setVideoURI(Uri.parse(video));
        videoView.requestFocus();
        videoView.start();
    }else {
        Toast.makeText(this, "No video found", Toast.LENGTH_SHORT).show();
    }

With scoped storage (required from API 30) you can't access files directly unless you request the MANAGE_EXTERNAL_STORAGE (on Google Play you need to request it to Google).

The new way is to use the file uri. You can try those ways:

  1. Ask the user to select the file.
private final ActivityResultLauncher<String[]> openDoc =
    registerForActivityResult(new ActivityResultContracts.OpenDocument(),
      new ActivityResultCallback<Uri>() {
        @Override
        public void onActivityResult(Uri uri) {
          // use uri
        }
      });

Call it with:

// Use the mimetype you want (optional). Like "text/plain"
openDoc.launch(new String[]{"text/plain"});

Read more here

  1. Get the Media file uri with MediaStore

Read more here

You'll also need the READ_EXTERNAL_STORAGE permission if the file was not created by your app.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM