如何使用Intent.ACTION_GET_CONTENT过滤掉不需要的文件 [英] How to filter out unwanted files using Intent.ACTION_GET_CONTENT

查看:608
本文介绍了如何使用Intent.ACTION_GET_CONTENT过滤掉不需要的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用intent.ACTION_GET_CONTENT允许用户仅选择图像或视频文件.这就是准备我的意图的方式

I'm using intent.ACTION_GET_CONTENT to allow a user to select images or video files only. this is the way am preparing my intent

 Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
 intent.setType("image/* | video/*");
 intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
 intent.addCategory(Intent.CATEGORY_OPENABLE);
 XActivity.startActivityForResult(intent, ACQUIRE_IMAGE_AND_VIDEOS_CODE);

当我单击启动此意图的按钮时,以下应用将显示在我的自定义对话框中

When i click the button that start this intent, the following apps are display in my customized dialog

下面是使用我的对话框填充可以处理上述意图的应用程序的方法,以及单击该对话框中的任何应用程序会发生什么情况

below is the method that fills my dialog with apps that can handle the above intent and also what happens when one clicks any app in the dialog

private void inflateDialog(List<ResolveInfo> intents){

ListView lv = (ListView) dialog.findViewById(R.id.listView1);

Collections.sort(intents,
        new ResolveInfo.DisplayNameComparator(packageManager));

appAdapter = new AppAdapter(packageManager, intents);

lv.setAdapter(appAdapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                            long arg3) {
        // TODO Auto-generated method stub
        ResolveInfo launchable = appAdapter.getItem(position);
        ActivityInfo activity = launchable.activityInfo;
        ComponentName name = new ComponentName(activity.applicationInfo.packageName,
                activity.name);
        IntentFilter filter = launchable.filter;

        Iterator<String> actions = filter.actionsIterator();

        Intent intent;

            if (filter.hasAction(Intent.ACTION_GET_CONTENT)) {
                intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("image/* | video/*");
                intent.setComponent(name);
                intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
                intent.addCategory(Intent.CATEGORY_OPENABLE);
                xActivity.startActivityForResult(intent, ACQUIRE_IMAGE_AND_VIDEOS_REQUEST_CODE);
                dialog.dismiss();
            }
});
dialog.show();

}

当我单击厨房应用程序时,我可以看到图像和视频并选择任何一个. 同时,我收到一个Toast告诉我有0张图片/视频.

When i click on galley app,i can see images and videos and select any. AT the same time, i receive a Toast telling me 0 images/videos available.

当我单击文件管理器和Google驱动器时,将显示所有文件. 没有正在过滤的图像和视频.

When i click file manager and google drive, am presented with all files. there is no filtering of images and videos that is taking place.

问题

  • 如何在文件管理器和Google Drive应用中强制过滤图像和视频?

  • How can i force filtering of images and videos in file manager and google drive apps?

为什么我可以选择在厨房上的烤面包 0个可用的图像/视频?

Why am i receiving the toast 0 images/videos available on galley even though i can select images and videos?

推荐答案

这是我发现过滤出不需要的文件并处理所需文件的唯一方法.就我而言,我只想要视频和图像.

This is the only way i could find to filter out unwanted files and deal with the ones i want. for my case, i only wanted videos and images.

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);

    if (resultCode == RESULT_OK) {

       String filePath;
       Uri file = intent.getData();
       String mimeType ="";

        if(file.getScheme().equals(ContentResolver.SCHEME_CONTENT)){

            mimeType = ctx.getContentResolver().getType(selectedImage);

          }else{
           String fileExtension = MimeTypeMap.getFileExtensionFromUrl(file.toString());
           mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension.toLowerCase());
          }


           if(mimeType.startsWith("video/")){
             //do something. for my case i get video path
              filePath = getFilePath(file);


             }else if(mimeType.startsWith("image/")){
              //do something. for my case i get image path
               filePath = getFilePath(file);
             }
       }
 }

我的getFilePath方法

my getFilePath method

private String getFilePath(Uri fileuri){
String filePath = "";

if (fileuri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) {
  Cursor cursor = this.getContentResolver().query(fileuri, new String[]{android.provider.MediaStore.Images.ImageColumns.DATA}, null, null, null);
  cursor.moveToFirst();
  filePath = cursor.getString(0);

  cursor.close();
} else {
  filePath = fileuri.getPath();
}
  return filePath;
}

这篇关于如何使用Intent.ACTION_GET_CONTENT过滤掉不需要的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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