How to filter out unwanted files using Intent.ACTION_GET_CONTENT

*爱你&永不变心* 提交于 2019-12-05 18:52:53

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