我如何排除一个特定的路径从搜索通过managedQuery使用游标android



大家好,我是android新手& &;

我应该做的是搜索存储在设备的SD卡中的所有视频(mp4格式)。这里我需要坚持的唯一条件是,它不应该查看指定的文件夹&它是子孩子,但只是其他人。到目前为止,我所做的带来的结果是存储在SD卡上的所有视频,包括要排除的文件夹。一旦实现了这一点,我必须从缩略图中选择文件&检索路径&对它执行任何进一步的操作。如何从指定的内容Uri中排除任何特定路径?下面是我的代码

在这方面的任何帮助都将是非常感谢的。

作为参考,我也研究了这个查询,从sd卡文件夹在listview中显示视频文件但不幸的是没有解决我的问题,因为我不得不坚持使用managedQuery的概念。

public class BrowseActivity extends Activity {
//set constants for MediaStore to query, and show videos
private final static Uri MEDIA_EXTERNAL_CONTENT_URI = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
private final static String _ID = MediaStore.Video.Media._ID;
private final static String MEDIA_DATA = MediaStore.Video.Media.DATA;
private GridView galleryView;
private Cursor cursor;
private int columnIndex;
private int[] videosId;
private Uri contentUri;
protected Context context;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    context = getApplicationContext();
    setContentView(R.layout.browse);
    //set GridView for gallery
    galleryView = (GridView) findViewById(R.id.gridview);
    //set default as external/sdcard uri
    contentUri = MEDIA_EXTERNAL_CONTENT_URI;
    showToast("contentUri is---------:- "+contentUri);
    //initialize the videos uri
    initVideosId();
    //set gallery adapter
    setGalleryAdapter();
}
private void setGalleryAdapter() {
    galleryView.setAdapter(new VideoGalleryAdapter(context));
    galleryView.setOnItemClickListener(_itemClickLis);
    showToast("Gallery Adapter Set ");
}
private AdapterView.OnItemClickListener _itemClickLis = new OnItemClickListener() {
    public void onItemClick(AdapterView parent, View v, int position, long id) {
        System.gc();
        // Now we want to actually get the data location of the file
        String [] proj={MEDIA_DATA};
        // We request our cursor again
        cursor = managedQuery(contentUri,
                proj,   // Which columns to return
                null,   // WHERE clause; which rows to return (all rows)
                null,   // WHERE clause selection arguments (none)
                null);  // Order-by clause (ascending by name)
        // We want to get the column index for the data uri
        // int count = cursor.getCount();
        cursor.moveToFirst();
        columnIndex = cursor.getColumnIndex(MEDIA_DATA);
        // Lets move to the selected item in the cursor
        cursor.moveToPosition(position);
        String filename = cursor.getString(columnIndex);
        showToast("Your Selection is:- "+filename);
    }
};
private void initVideosId() {
    try {           
        //Here we set up a string array of the thumbnail ID column we want to get back
        String [] proj={_ID};
        // Now we create the cursor pointing to the external thumbnail store
        cursor = managedQuery(contentUri,
                proj,   // Which columns to return
                null,   // WHERE clause; which rows to return (all rows)
                null,   // WHERE clause selection arguments (none)
                null);  // Order-by clause (ascending by name)
        int count= cursor.getCount();
        // We now get the column index of the thumbnail id
        columnIndex = cursor.getColumnIndex(_ID);
        videosId = new int[count];
        //move position to first element
        cursor.moveToFirst();
        for(int i=0;i<count;i++) {
            int id = cursor.getInt(columnIndex);
            videosId[i]= id;
            cursor.moveToNext();
        }
    } catch(Exception ex) {
        showToast(ex.getMessage().toString());
    }
}
protected void showToast(String msg) {
    Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
}
private class VideoGalleryAdapter extends BaseAdapter
{
    public VideoGalleryAdapter(Context c) {
        context = c;
    }
    public int getCount() {
        return videosId.length;
    }
    public Object getItem(int position) {
        return position;
    }
    public long getItemId(int position) {
        return position;
    }
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imgVw= new ImageView(context);
        try {
            if(convertView!=null) {
                imgVw= (ImageView) convertView;
            }
            imgVw.setImageBitmap(getImage(videosId[position]));
            imgVw.setLayoutParams(new GridView.LayoutParams(96, 96));
            imgVw.setPadding(8, 8, 8, 8);
        } catch(Exception ex) {
            System.out.println("BrowseActivity:getView()-135: ex " + ex.getClass() +", "+ ex.getMessage());
        }
        return imgVw;
    }
    // Create the thumbnail on the fly
    private Bitmap getImage(int id) {
        Bitmap thumb = MediaStore.Video.Thumbnails.getThumbnail(
                getContentResolver(),
                id, MediaStore.Video.Thumbnails.MICRO_KIND, null);
        return thumb;
    }
}

感谢CommonWare提供的宝贵建议,我要求您提供示例where子句的原因是我只是这样,我不知道应该在where子句中提到什么,如列名等

然而,经过许多值得充分的努力,我终于有了一个工作的例子& &;完全达到了我的目的。现在我可以查询设备,不包括任何特定的文件夹。我只是想和大家分享一下。

我刚刚在我的查询被触发

cursor = managedQuery(contentUri,
            proj,
            MediaStore.Images.Media.DATA + " not like ? ",
            new String[] {"%excludeFolder%"},
            null);

其中%excludeFolder%指的是路径例如/mnt/sdcard/excludeFolder &它的子节点。还有一点需要注意的是,查询不像,所以它从搜索中排除了这个。

然而,这是我实现的方式,如果有任何其他更好的选择,这个问题仍然开放给你宝贵的答复。

*PS:-如果你喜欢它,请给它打分/投票&它解决了你的担忧。*

谢谢。

最新更新