1

我创建了一个 android 程序来从 sdcard 的任何特定文件夹中读取视频,然后在网格视图中显示它们的缩略图。但是,我的程序仅适用于 sdcard 中的 Movies 文件夹。它没有显示 sdcard 中其他文件夹的任何视频。

我在代码中的两个位置指定了文件夹“电影”的名称。一次在 AdapterView.OnItemClickListener 中,另一个在 initVideosId() 中。

现在我的 sdcard 中还有其他文件夹,比如 sdcard->geovideo

现在,当我指定“geovideo”而不是“电影”时,它在我的平板电脑中什么也没有显示。你知道有什么问题吗。

任何人都请帮助我完成我的任务。谢谢。下面是我的安卓程序

package com.vt.soc;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
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.GridView;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;


public class MainActivity 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;
    //flag for which one is used for images selection
    private GridView _gallery; 
    private Cursor _cursor;
    private int _columnIndex;
    private int[] _videosId;
    private Uri _contentUri;
    String filename;
    int flag = 0;



    protected Context _context;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        _context = getApplicationContext();
        setContentView(R.layout.activity_main);
        //set GridView for gallery
        _gallery = (GridView) findViewById(R.id.videoGrdVw);
        //set default as external/sdcard uri
        _contentUri = MEDIA_EXTERNAL_CONTENT_URI;

        initVideosId();

        //set gallery adapter
        setGalleryAdapter();


    }
    private void setGalleryAdapter() {
        _gallery.setAdapter(new VideoGalleryAdapter(_context));
        _gallery.setOnItemClickListener(_itemClickLis);
        flag = 1;       
    }
    private AdapterView.OnItemClickListener _itemClickLis = new OnItemClickListener() 
    {
        @SuppressWarnings({ "deprecation", "unused", "rawtypes" })
        public void onItemClick(AdapterView parent, View v, int position, long id) 
        {
            // 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
                    MEDIA_DATA + " like ? ",       // WHERE clause; which rows to return (all rows)
                    new String[] {"%Movies%"},       // 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);
            // And here we get the filename
            filename = _cursor.getString(_columnIndex);
            //*********** You can do anything when you know the file path :-)
            showToast(filename);

            Intent i = new Intent(MainActivity.this, Player.class); 
            i.putExtra("videoPath", filename);
            startActivity(i);


            //
        }
    };
    @SuppressWarnings("deprecation")
    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
                    MEDIA_DATA + " like ? ",       // WHERE clause; which rows to return (all rows)
                    new String[] {"%Movies%"},       // 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);
            //initialize 
            _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(200, 200));
                imgVw.setPadding(8, 8, 8, 8);
            }
            catch(Exception ex)
            {
                System.out.println("MainActivity: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;
        }

    }


}
4

1 回答 1

1

实际上问题是我的清单文件中的用户权限。

我希望任何面临与我相同问题的人都会很高兴看到我的回答。

实际上,电影文件夹在 sdcard 的内置文件夹中,正如我提到的,我在 sdcard 中有自己的文件夹“geovideo”,我无法通过我的程序看到。所以我需要在 Manifest 文件中指定权限。

当我添加

 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

我的清单。它正在工作。所以只是简单的建议,当你的程序不工作时,也要考虑许可。

于 2012-09-25T06:17:08.910 回答