How to set android intent for several file types (pdf, office, images, text) and return the path?

I'm new to intentions, and I'm trying to figure out how to use parsing (URI) and / or setType () to get the right types of applications to open and let me choose things.

I want to start the intention of your application, which will allow the user to select one of the many file types ( .PDF, .DOCX, .XLSX, .PPTX, .DOC, .JPG, .PNG, .TXT, .LOG, etc.). I need an operation to return - this is the full path to this file.

I am currently using setType("*/*")with the selection I found here, but this automatically opens up some document selector in Android. I have a file manager and other applications, and you want to know what the standard setType type or MIME type is. Thanks in advance.

In addition, I apologize if this has already been answered. I looked online, but I think I was looking for the wrong thing, because the results that I get are for intentions that simply want one of them or do not return the path.

My applicable code is below: (Note: this is done inside the snippet)

static final int PICK_FILE_REQUEST = 101;

private String pathToFile = "";

public String selectFile()  {
    String path = "";
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT), chooser = null;
    intent.setType("*/*");
    chooser = Intent.createChooser(intent, "Find file to Print");
    startActivityForResult(chooser, PICK_FILE_REQUEST);
    path = pathToFile;
    return path;
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)   {
    if(requestCode == PICK_FILE_REQUEST){
        if(resultCode == Activity.RESULT_OK){
            pathToFile = data.getDataString();
            String temp = data.getStringExtra("path");
            Log.d("Files Fragment: ", pathToFile);
            Log.d("Files Fragment: ", temp);
        }
    }
}
+4
source share
4 answers

You can use Intent.ACTION_OPEN_DOCUMENT,

URI ://, DocumentProvider, openFileDescriptor (Uri, String) DocumentContract.Document.

. , takePersistableUriPermission (Uri, int).

MIME setType (String). , , /*. MIME-, EXTRA_MIME_TYPES setType (String) */*.

. .

+5

, 15 , EXTRA_MIME_TYPES

, http://android-er.blogspot.co.uk/2015/09/open-multi-files-using.html, , , , , , , mime ( - csv mime, USB, , Google ), . , mime- .

public void findReviewsToLoad() {
    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    String [] mimeTypes = {"text/csv", "text/comma-separated-values"};
    intent.setType("*/*");
    intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
    startActivityForResult(intent, FIND_FILE_REQUEST_CODE);
}
+7
private static final int CHOOSE_FILE_REQUEST = 1;
///////////////////////////////////////////////////
 public void chooseFile(){
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.setType("*/*");
        String[] extraMimeTypes = {"application/pdf", "application/doc"};
        intent.putExtra(Intent.EXTRA_MIME_TYPES, extraMimeTypes);
        intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
        startActivityForResult(intent, CHOOSE_FILE_REQUEST);


    }

/////////////////////////////////////////////////////

   @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        String path = "";
        if (resultCode == RESULT_OK) {
            if (requestCode == CHOOSE_FILE_REQUEST) {
                ClipData clipData = data.getClipData();
                //null and not null path
                if(clipData == null){
                    path += data.getData().toString();
                }else{
                    for(int i=0; i<clipData.getItemCount(); i++){
                        ClipData.Item item = clipData.getItemAt(i);
                        Uri uri = item.getUri();
                        path += uri.toString() + "\n";
                    }
                }
            }
        }
        selectedFileTV.setText(path);
    }
+1

The following code works for me when you need to select multiple MIME types (this is in C # and Xamarin for Android):

List<string> mimeTypes = new List<string>();

// Filling mimeTypes with needed MIME-type strings, with * allowed.

if (mimeTypes.Count > 0)
{
    // Specify first MIME type
    intent.SetType(mimeTypes[0]);
    mimeTypes.RemoveAt(0);
}
if (mimeTypes.Count > 0)
{
    // Specify the rest MIME types but the first if any
    intent.PutExtra(Intent.ExtraMimeTypes, mimeTypes.ToArray());
}

Please note, I tried to use intent.SetType("*.*")As described in other answers, but this led to incorrect behavior.
For example, when I implement a code to select photos from a gallery, a video where they were also selected:

intent.SetType("*.*");
intent.PutExtra(Intent.ExtraMimeTypes, new string[] {"image/*"});

Therefore, I have intent.SetTypethe above code: specify the first MIME type in intent.SetType, and everything else - throughintent.PutExtra(Intent.ExtraMimeTypes,...)

0
source

All Articles