【问题标题】:Selecting a specific type of file in android在android中选择特定类型的文件
【发布时间】:2016-03-10 11:46:15
【问题描述】:

我正在尝试制作一个应用程序,用户可以在其中使用 android 设备的文件管理器选择文件。这是我正在使用的代码:

 boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
                    if (isKitKat)
                    {
                        Intent uploadIntent = new Intent();
                        uploadIntent.setType("*/*");
                        uploadIntent.setAction(Intent.ACTION_GET_CONTENT);
                        startActivityForResult(uploadIntent, REPORT_USING_FILE_MANAGER);

                    }
                    else
                    {
                        Intent uploadIntent = new Intent(Intent.ACTION_GET_CONTENT);
                        uploadIntent.setType("*/*");
                        startActivityForResult(uploadIntent, REPORT_USING_FILE_MANAGER);
                    }

我只希望用户能够选择图像文件(.jpg、.png 等)或 .pdf 文件。如何设置此限制?

【问题讨论】:

    标签: android android-intent file-manager


    【解决方案1】:

    您正在使用loadIntent.setType("*/*"); 选择所有类型的文件。

    您必须使用 MIME 类型对其进行过滤。

    例如,如果您想选择 jpeg 文件,那么

    loadIntent.setType("image/jpeg");
    

    一些MIME类型如下,

    image/jpeg
    audio/mpeg4-generic
    text/html
    audio/mpeg
    audio/aac
    audio/wav
    audio/ogg
    audio/midi
    audio/x-ms-wma
    video/mp4
    video/x-msvideo
    video/x-ms-wmv
    image/png
    image/jpeg
    image/gif
    .xml ->text/xml
    .txt -> text/plain
    .cfg -> text/plain
    .csv -> text/plain
    .conf -> text/plain
    .rc -> text/plain
    .htm -> text/html
    .html -> text/html
    .pdf -> application/pdf
    .apk -> application/vnd.android.package-archive
    

    如果您有任何问题,请告诉我。

    【讨论】:

    • 我使用哪种类型的 Excel 文件
    【解决方案2】:

    你需要为图片文件实现“photoPickerIntent”

    Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
    photoPickerIntent.setType("image/*");
    startActivityForResult(photoPickerIntent, SELECT_PHOTO);
    

    之后应该做以下事情:

    @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent intent)
        {
            switch (requestCode)
            {
                case SELECT_PHOTO:
                    if (resultCode == RESULT_OK)
                    {
                        Uri selectedImage = intent.getData();
                        System.out.println(selectedImage.toString() + "-" + selectedImage.getPath());
    
                        String[] filePathColumn = {MediaStore.Images.Media.DATA};
                        // Get the cursor
                        Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                        // Move to first row
                        cursor.moveToFirst();
    
                        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
    
                        imgDecodableString = cursor.getString(columnIndex);
                        cursor.close();
    
                    }
                    break;
                default:
                    break;
            }
    
        }
    

    【讨论】:

      猜你喜欢
      • 2013-07-13
      • 1970-01-01
      • 2021-09-11
      • 1970-01-01
      • 2011-11-26
      • 2018-07-30
      • 1970-01-01
      • 2021-09-29
      • 2018-10-27
      相关资源
      最近更新 更多