Such a problem, I try to play video files from a folder, by clicking on the video file, the video player starts up, and then an error is written: the video is not supported. The video is correct and error-free, recorded from the camera of the same smartphone (files are played, entered through total in the folder from which I am trying to play and the video was played by a standard player)
method for finding files with the extension mp4:
public ArrayList<String> getVideoFiles(String directoryPath) { ArrayList<String> result = new ArrayList<>(); File directory = new File(directoryPath); File files[] = directory.listFiles(new FilenameFilter() { @Override public boolean accept(File dir, String name) { return name.endsWith(".MP4"); } }); for (File file : files) { result.add(file.getAbsoluteFile().toString()); } return result; } I call the method here:
@Override public void onBindViewHolder(VideoPreviewHolder holder, int position) { holder.mVideoPreviewImageView.setImageBitmap(mItems.get(position).getVideoBitmap()); holder.mVideoPreviewTextView.setText(mItems.get(position).getVideoName()); holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { PreviewActivity previewActivity = new PreviewActivity(); ArrayList<String> files = previewActivity .getVideoFiles(Environment.getExternalStorageDirectory() + "/.OtherFile"); Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse( String.valueOf(files))); intent.setDataAndType(Uri.parse(String.valueOf(Environment.getExternalStorageDirectory() + "/.OtherFile")), "video/MP4"); v.getContext().startActivity(intent); } }); } Combinations with: "video / mp4" / "video / MP4" and return name.endsWith (". MP4"); / return name.endsWith (". Mp4"); don't solve problems.
UPDATE:
public class VideoPreviewAdapter extends RecyclerView.Adapter<VideoPreviewAdapter.VideoPreviewHolder> { private Context mContext; private ArrayList<VideoPreview> mItems; private List<String> path_vid; public VideoPreviewAdapter(Context context) { mContext = context; mItems = new ArrayList<>(); } public void addItems(ArrayList<VideoPreview> items) { mItems.addAll(items); notifyDataSetChanged(); } public void clearItems() { mItems.clear(); } @Override public VideoPreviewHolder onCreateViewHolder(ViewGroup parent, int viewType) { LayoutInflater layoutInflater = LayoutInflater.from(mContext); View view = layoutInflater.inflate(R.layout.video_preview_item, parent, false); return new VideoPreviewHolder(view); } /* public void searchVid(File dir) { String pattern = ".MP4"; //Get the listfile of that flder final File listFile[] = dir.listFiles(); if (listFile != null) { for (int i = 0; i < listFile.length; i++) { final int x = i; if (listFile[i].isDirectory()) { walkdir(listFile[i]); } else { if (listFile[i].getName().endsWith(pattern)) { // Do what ever u want, add the path of the video to the list path_vid.add(listFile[i]); } } } } }*/ @Override public void onBindViewHolder(VideoPreviewHolder holder, int position) { holder.mVideoPreviewImageView.setImageBitmap(mItems.get(position).getVideoBitmap()); holder.mVideoPreviewTextView.setText(mItems.get(position).getVideoName()); holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { /* PreviewActivity previewActivity = new PreviewActivity(); ArrayList<String> files = previewActivity .getVideoFiles(Environment.getExternalStorageDirectory() + "/.OtherFile"); for (String fileArray : files) { }*/ String path = Environment.getExternalStorageDirectory() + "/.OtherFile"; Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(String.valueOf(path))); intent.setDataAndType(Uri.parse( String.valueOf(Environment.getExternalStorageDirectory() + "/.OtherFile")), "video/MP4"); v.getContext().startActivity(intent); } }); } @Override public int getItemCount() { return mItems.size(); } public class VideoPreviewHolder extends RecyclerView.ViewHolder { public ImageView mVideoPreviewImageView; public TextView mVideoPreviewTextView; public VideoPreviewHolder(View itemView) { super(itemView); mVideoPreviewImageView = (ImageView) itemView.findViewById(R.id.video_preview_image_view); mVideoPreviewTextView = (TextView) itemView.findViewById(R.id.video_preview_text_view); } } }
positionin theonBindViewHolder(...)method. With the help ofpositionyou can get a specific object and a very specific path to the selected video file from your data set. - post_zeew pmaddItems(...)method is used, that is, after adding elements, these elements will be inmItems. Each element contains, in particular, the path to a specific video file . In theonBindViewHolder(...)method, you can get the selected item by calling theget(position)mItemsobject. And then from this element you can get the path to the file using the corresponding getter. - post_zeew