Sunday, September 6, 2015

Open multi files using Intent.ACTION_OPEN_DOCUMENT, with EXTRA_ALLOW_MULTIPLE and getClipData()

Example to open multi files using Intent.ACTION_OPEN_DOCUMENT, with EXTRA_ALLOW_MULTIPLE, and retrieve Uri with getClipData().


com.blogspot.android_er.android_action_open_document.MainActivity.java
package com.blogspot.android_er.android_action_open_document;

import android.content.ClipData;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private static final int RQS_OPEN = 1;

    Button buttonOpen;
    TextView textUri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        buttonOpen = (Button) findViewById(R.id.opendocument);
        buttonOpen.setOnClickListener(buttonOpenOnClickListener);

        textUri = (TextView) findViewById(R.id.texturi);
    }

    View.OnClickListener buttonOpenOnClickListener =
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    //Open multi-type using Intent.ACTION_OPEN_DOCUMENT
                    //Open multi-file
                    Intent intent = new Intent();
                    intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
                    intent.addCategory(Intent.CATEGORY_OPENABLE);
                    intent.setType("*/*");
                    String[] extraMimeTypes = {"image/*", "video/*"};
                    intent.putExtra(Intent.EXTRA_MIME_TYPES, extraMimeTypes);
                    intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
                    startActivityForResult(intent, RQS_OPEN);

                    Toast.makeText(MainActivity.this,
                            "Single-selection: Tap on any file.\n" +
                                    "Multi-selection: Tap & Hold on the first file, " +
                                    "tap for more, tap on OPEN to finish.",
                            Toast.LENGTH_LONG).show();
                }
            };

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

        String s = "";
        if (resultCode == RESULT_OK) {
            if (requestCode == RQS_OPEN) {
                ClipData clipData = data.getClipData();

                //Both approach work

                if(clipData == null){
                    s = "clipData == null\n";
                    s += data.getData().toString();
                }else{
                    s = "clipData != null\n";
                    for(int i=0; i<clipData.getItemCount(); i++){
                        ClipData.Item item = clipData.getItemAt(i);
                        Uri uri = item.getUri();
                        s += uri.toString() + "\n";
                    }
                }

                /*
                if(data.getData() == null){
                    s = "data.getData() == null\n";
                    for(int i=0; i<clipData.getItemCount(); i++){
                        ClipData.Item item = clipData.getItemAt(i);
                        Uri uri = item.getUri();
                        s += uri.toString() + "\n";
                    }
                }else{
                    s = "data.getData() != null\n";
                    s += data.getData().toString();
                }
                */
            }
        }
        textUri.setText(s);
    }
}


Layout file refer to last exercise "Using Intent.ACTION_OPEN_DOCUMENT with EXTRA_MIME_TYPES to open file of multi mime types".


Next:
Intent.ACTION_OPEN_DOCUMENT to load images in RecyclerView + CardView

1 comment:

Anonymous said...

gracias