如何获取Android中特定文件夹中的视频文件列表?

mwkjh3gx  于 2023-01-28  发布在  Android
关注(0)|答案(5)|浏览(336)

如何获得使用MediaStore存储在特定文件夹中的视频文件列表?目前我正在使用此代码

package com.example.videolisttest;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

public class MainActivity extends Activity {
    private Cursor videocursor;
    private int video_column_index;
    ListView videolist;
    int count;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init_phone_video_grid();
    }

    private void init_phone_video_grid() {
        System.gc();
        String[] proj = { MediaStore.Video.Media._ID,
                MediaStore.Video.Media.DATA,
                MediaStore.Video.Media.DISPLAY_NAME,
                MediaStore.Video.Media.SIZE };
        videocursor = managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
                proj, null, null, null);
        count = videocursor.getCount();
        videolist = (ListView) findViewById(R.id.PhoneVideoList);
        videolist.setAdapter(new VideoAdapter(getApplicationContext()));
        videolist.setOnItemClickListener(videogridlistener);
    }

    private OnItemClickListener videogridlistener = new OnItemClickListener() {
        public void onItemClick(AdapterView parent, View v, int position,
                long id) {
            System.gc();
            video_column_index = videocursor
                    .getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
            videocursor.moveToPosition(position);
            String filename = videocursor.getString(video_column_index); 
            /*   Intent intent = new Intent(MainActivity.this, ViewVideo.class);
                  intent.putExtra("videofilename", filename);
                  startActivity(intent);*/
            Toast.makeText(getApplicationContext(), filename, Toast.LENGTH_SHORT).show();
        }
    };

    public class VideoAdapter extends BaseAdapter {
        private Context vContext;

        public VideoAdapter(Context c) {
            vContext = c;
        }

        public int getCount() {
            return count;
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            System.gc();
            TextView tv = new TextView(vContext.getApplicationContext());
            String id = null;
            if (convertView == null) {
                video_column_index = videocursor
                        .getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME);
                videocursor.moveToPosition(position);
                id = videocursor.getString(video_column_index);
                video_column_index = videocursor
                        .getColumnIndexOrThrow(MediaStore.Video.Media.SIZE);
                videocursor.moveToPosition(position);
                id += " Size(KB):" + videocursor.getString(video_column_index);
                tv.setText(id);
            } else
                tv = (TextView) convertView;
            return tv;
        }
    }
}

但是这个方法扫描整个sdcard。我想扫描一个特定的文件夹。我不想使用filefilter方法。请分享任何使用mediastore的方法。

vsnjm48y

vsnjm48y1#

你可以使用此代码从特定文件夹获取视频为:

String selection=MediaStore.Video.Media.DATA +" like?";
            String[] selectionArgs=new String[]{"%FolderName%"};
            videocursor = managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
                    parameters, selection, selectionArgs, MediaStore.Video.Media.DATE_TAKEN + " DESC");

它对我来说就像一种魅力。

ulydmbyx

ulydmbyx2#

private List<String> path_vid;
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]);
                }
            }
        }
    }
}

这个函数是递归的,搜索视频设置成一个列表,视频从指定的文件夹中搜索,如果你想搜索视频到sdCard,用途:

File sdCard = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
//For example:
//File vidsFolder= new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Videos");
searchVid(sdCard);
if(path_vid.size()>0){
   //Convert list into array
   String[] array = path_vid.toArray(new String[path_vid.size()]);
   //Create Adapter
   ArrayAdapter<String> adapter =new ArrayAdapter<String>(this,android.R.layout.simple_list_item, array);
   //Set adapter to videlist
   videolist.setAdapter(adapter);
}else{
   //No vids found
   exit();
}
cclgggtu

cclgggtu3#

如果您知道特定的文件夹,请用途:

.getExternalFilesDir
lf3rwulv

lf3rwulv4#

@要求Api(api =构建版本代码果冻豆)
公共空getVideos(){

ContentResolver contentResolver = getContentResolver();
    Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
    String selection = MediaStore.Video.Media.DATA +" like?";
    String[] selectionArgs = new String[]{"%/" + "Download" + "/%"};

    Cursor cursor = contentResolver.query(uri, null, selection, selectionArgs , null);

    //looping through all rows and adding to list
    if (cursor != null && cursor.moveToFirst()) {
        do {
            String title = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.TITLE));
            String duration = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.DURATION));
            String data = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.DATA));
            VideoModel videoModel = new VideoModel();
            videoModel.setVideoTitle(title);
            videoModel.setVideoUri(Uri.parse(data));
            videoModel.setVideoDuration(timeConversion(Long.parseLong(duration)));
            videoArrayList.add(videoModel);

        } while (cursor.moveToNext());
    }

    VideoAdapter adapter = new VideoAdapter(this, videoArrayList);
    recyclerView.setAdapter(adapter);

    adapter.setOnItemClickListener(new VideoAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(int pos, View v) {
            Intent intent = new Intent(getApplicationContext(), VideoPlayActivity.class);
            intent.putExtra("pos", pos);
            startActivity(intent);
        }
    });

}

对我来说效果很好。
完整代码https://github.com/PSOjha/PlayVideoFromSDCardFolder

zpf6vheq

zpf6vheq5#

在您的设备上加载所有视频

String[] projection = {MediaStore.Video.Media._ID, MediaStore.Video.Media.DISPLAY_NAME, MediaStore.Video.Media.DURATION};
            String sortOrder = MediaStore.Video.Media.DATE_ADDED + " DESC";
            Cursor cursor = getApplication().getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
                    projection, null, null, sortOrder);

仅加载特定文件夹视频

String selection=MediaStore.Video.Media.DATA +" like?";
            String[] selectionArgs=new String[]{"%Videoder%"};
           Cursor cursor = managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
                    projection,selection,selectionArgs, MediaStore.Video.Media.DATE_TAKEN + " DESC");

相关问题