How to filter out unwanted files using Intent.ACTION_GET_CONTENT

我的未来我决定 提交于 2019-12-07 16:42:41

问题


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();

}

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.

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.

QUESTIONS

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

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


回答1:


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);
             }
       }
 }

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;
}


来源:https://stackoverflow.com/questions/36397670/how-to-filter-out-unwanted-files-using-intent-action-get-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!