Pick any file using intent in android

3.1k views Asked by At

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

    }

Following permissions are set in manifest

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

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.enter image description here Without method in it button click works fine. If anyone found errors in code please let me know.

Thanks

2

There are 2 answers

0
Hasmukh Kachhatiya On

try this code this my code only for .xls use yours. or add read and write permission in manifest file

oncreate declare permission

 if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.WRITE_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED) {
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
        } else {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},23
            );
        }
    }

onclick of you button write this code

path= String.valueOf(Environment.getExternalStorageDirectory());
                File file = new File(path);
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setDataAndType(Uri.fromFile(file), "application/vnd.ms-excel");

                try {
                    startActivityForResult(pdfOpenintent.createChooser(intent, "Select file"), 0);                }
                catch (ActivityNotFoundException e) {

                }

onactivityresult yud get file path

public void onActivityResult(int requestCode, int resultCode, Intent result){
        if (resultCode == RESULT_OK){
            if (requestCode == 0) {
                Uri data = result.getData();

                else{
                    //  CommonMethods.ShowMessageBox(CraneTrackActivity.this, "Invalid file type");
                    Toast.makeText(Import_act.this,"Wrong File Selected ", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }
0
Kyaw Kyaw On

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.