在Android中使用Intent选择任何文件 [英] Pick any file using intent in android

查看:232
本文介绍了在Android中使用Intent选择任何文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下方法来调用选择任何文件,但是它无法正常工作.

I'm using following method to call pick any file but it doesn't work properly.

private void fileIntent(int file)
    {
        if ((ActivityCompat.checkSelfPermission(ICShowFileCabinetDetails.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, file);
        } else {
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType("*/*");
            startActivityForResult(Intent.createChooser(intent, "Select File"), file);
        }

    }

以下权限在清单中设置

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

OnActivtyresult

OnActivtyresult

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 if (requestCode == SELECT_FILE && data != null) {
            try {
                mProPic = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData());

                Uri selectedImage = data.getData();

                String[] filePathColumn = {MediaStore.Images.Media.DATA};

                Cursor cursor = getContentResolver().query(selectedImage,
                        filePathColumn, null, null, null);
                cursor.moveToFirst();
                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String picturePath = cursor.getString(columnIndex);

//                String filename = selectedImage.getLastPathSegment();

                String[] filenames = picturePath.split("\\/");


                int count = filenames.length;
                String name = filenames[count - 1];


                imagepickerselected = 1;
                UploadIamgeinServer(1, name);

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
}

每当我单击选择按钮单击中的文件时,选择打开文件.但是所有文件都显示为隐藏状态,但图像除外,单击不起作用.如果不使用方法,则单击按钮即可正常工作.如果有人发现代码错误,请通知我.

Choose file getting open whenever i click to choose file in button click. But all files are shown like hidden except images, Click doest work. Without method in it button click works fine. If anyone found errors in code please let me know.

谢谢

推荐答案

我认为您应该像这样添加intent.addCategory(Intent.CATEGORY_OPENABLE); .有关更多信息,请访问此链接,通过Intent从文件管理器中选择文件.希望对您有所帮助.

I think you should add like this intent.addCategory(Intent.CATEGORY_OPENABLE); . For more visit this link, Select File from file manager via Intent. I hope this may help you.

这篇关于在Android中使用Intent选择任何文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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