Can it is possible to allow user for multiple selection of file in storage access framework..?

3.2k views Asked by At

After clicking on a button i am getting the content from provider

 Intent i = new Intent(Intent.ACTION_OPEN_DOCUMENT);
        i.addCategory(Intent.CATEGORY_OPENABLE);
        i.setType("image/*");
        startActivityForResult(i, REQUESTCODE);

now i want to allow user for multiple selection is it possible.?

2

There are 2 answers

2
Thomaz Freitas Barbosa On

Don't know if you solved your problem, but here's how I implemented a multiple selection with the Storage Access Framework

    Intent filePickerIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
    filePickerIntent.setType("*/*");
    filePickerIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
    startActivityForResult(filePickerIntent, REQUEST_CODE);

In the Activity Result method, you just need to iterate the ClipData in the Intent parameter

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if(requestCode == REQUEST_CODE)
    {
        if(data != null)
        {
            ClipData clipData = data.getClipData();
            for(int i = 0; i < clipData.getItemCount(); i++)
            {
                ClipData.Item path = clipData.getItemAt(i);
                Log.i("Path:",path.toString());
            }
        }
    }
}

To select multiple files in the Storage Access Framework Activity UI, just hold press any item and the multi selection will activate.

0
Younes Belouche On

Here is what you need:

val PICK_JSON_FILES = 2
private fun pickFilesFromDevice() {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
    addCategory(Intent.CATEGORY_OPENABLE)
    putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)
    type = "application/json"
}
startActivityForResult(intent, PICK_JSON_FILES)
}

override fun onActivityResult(requestCode: Int, resultCode: Int, 
resultData: Intent?) {
super.onActivityResult(requestCode, resultCode, resultData)

if (requestCode == PICK_JSON_FILES  && resultCode == Activity.RESULT_OK) {
    // Check if multiple files were selected
    val clipData = resultData?.clipData
    if (clipData != null) {
        for (i in 0 until clipData.itemCount) {
            val uri = clipData.getItemAt(i).uri
            // Use the uri
        }
    } else {
        // If only one file was selected, handle it here
        val uri = resultData?.data
        // Use the uri
    }
  }
}

Note: the type of files I am looking for here is JSON, change it for the type you want.