带文件夹过滤器的图库 [英] Gallery with folder filter

查看:28
本文介绍了带文件夹过滤器的图库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码在我的应用程序内打开一个画廊

I'm using following code to open a gallery inside of my app

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, FIND_RESULT);

是否可以将图像列表限制为仅显示相机拍摄的图像?在我的 2.1 系统上查看图库,图像被分组,因此必须有一个参数来定义它属于哪个文件夹.

Is it possible to limit a list of images to only show images taken by camera? Viewing Gallery on my 2.1 system, images are grouped so there has to be a parameter that defines to which folder it belongs.

检查 MediaStore.Images.ImageColumns 我没有找到任何可以定义此类内容的列.

Checking the MediaStore.Images.ImageColumns I did not a find any column that would define such thing.

我会错吗?因为如果我可以创建一个查询来按文件夹过滤并创建我自己的图库视图,那么我的问题就迎刃而解了.

Could I be wrong? Because if I could create a query to filter by folder and create my own gallery view, then my problem would be solved.

推荐答案

您只需要在您的活动中实现 MediaScannerConnectionClient ,然后您必须在此处提供该文件夹名称中的文件之一的确切路径作为 SCAN_PATH 和它将扫描包含在该文件夹中的所有文件并在内置库中打开它.所以只要给你文件夹的名字,你就会得到里面的所有文件,包括视频.如果您只想打开图像更改 FILE_TYPE="image/*"

You just need to implement MediaScannerConnectionClient in your activity and after that you have to give the exact path of one of the file inside that folder name here as SCAN_PATH and it will scan all the files containing in that folder and open it inside built in gallery. So just give the name of you folder and you will get all the files inside including video. If you want to open only images change FILE_TYPE="image/*"

public class SlideShow extends Activity implements MediaScannerConnectionClient {

        public String[] allFiles;
        private String SCAN_PATH ;
        private static final String FILE_TYPE = "*/*";
        private MediaScannerConnection conn;

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);

            setContentView(R.layout.main);

            File folder = new File("/sdcard/yourfoldername/");
            allFiles = folder.list();

            SCAN_PATH=Environment.getExternalStorageDirectory().toString()+"/yourfoldername/"+allFiles[0];

            Button scanBtn = (Button) findViewById(R.id.scanBtn);
            scanBtn.setOnClickListener(new OnClickListener()
            {
                public void onClick(View v)
                {
                    startScan();
                }
            });
        }

        private void startScan()
        {
            if(conn!=null)
            {
                conn.disconnect();
            }

            conn = new MediaScannerConnection(this, this);
            conn.connect();
        }


        public void onMediaScannerConnected()
        {
            conn.scanFile(SCAN_PATH, FILE_TYPE);    
        }


        public void onScanCompleted(String path, Uri uri)
        {
            try
            {
                if (uri != null) 
                {
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setData(uri);
                    startActivity(intent);
                }
            }
            finally 
            {
                conn.disconnect();
                conn = null;
            }
        }
    }

这篇关于带文件夹过滤器的图库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆