Pick any file using intent in android
Asked Answered
P

2

1

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

Perturb answered 28/8, 2017 at 6:37 Comment(9)
can you update your log and what is your android version.. you are giving intent in elseCataplasm
Please post the logs?Doggery
Sorry friends question updated . App isn't crashing. Please refer pic attachment.Perturb
follow my answer you can use your file extension instead of xlsEmotionalism
@Hasmukhkachhatiya it works only for excel files right? My requirement is to choose any file.Perturb
no it work for any i just check the extension of fileEmotionalism
let's see my update answer i remove that confuse lineEmotionalism
@Hasmukhkachhatiya : it shows the same... I've attached a screenshot here , looks like hidden files in chooserPerturb
click on 3 dot overflow botton and click show sd card optionEmotionalism
E
0

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();
                }
            }
        }
    }
Emotionalism answered 28/8, 2017 at 6:45 Comment(0)
H
0

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.

Herwick answered 28/8, 2017 at 7:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.