Example usage for android.content Intent ACTION_CREATE_DOCUMENT

List of usage examples for android.content Intent ACTION_CREATE_DOCUMENT

Introduction

In this page you can find the example usage for android.content Intent ACTION_CREATE_DOCUMENT.

Prototype

String ACTION_CREATE_DOCUMENT

To view the source code for android.content Intent ACTION_CREATE_DOCUMENT.

Click Source Link

Document

Activity Action: Allow the user to create a new document.

Usage

From source file:org.de.jmg.learn.MainActivity.java

@SuppressLint("InlinedApi")
public void SaveVokAs(boolean blnUniCode, boolean blnNew) throws Exception {
    boolean blnActionCreateDocument = false;
    try {/*w w w.j  a  v a2  s.c  o m*/
        if (fPA.fragMain != null && fPA.fragMain.mainView != null)
            fPA.fragMain.EndEdit(false);
        if (!libString.IsNullOrEmpty(vok.getFileName()) || vok.getURI() == null || Build.VERSION.SDK_INT < 19) {
            boolean blnSuccess;
            for (int i = 0; i < 2; i++) {
                try {
                    String key = "AlwaysStartExternalProgram";
                    int AlwaysStartExternalProgram = prefs.getInt(key, 999);
                    lib.YesNoCheckResult res;
                    if (AlwaysStartExternalProgram == 999 && !(vok.getURI() != null && i == 1)) {
                        res = lib.ShowMessageYesNoWithCheckbox(this, "",
                                getString(R.string.msgStartExternalProgram),
                                getString(R.string.msgRememberChoice), false);
                        if (res != null) {
                            if (res.res == yesnoundefined.undefined)
                                return;
                            if (res.checked)
                                prefs.edit().putInt(key, res.res == yesnoundefined.yes ? -1 : 0).commit();
                        } else {
                            yesnoundefined par = yesnoundefined.undefined;
                            res = new lib.YesNoCheckResult(par, true);
                        }
                    } else {
                        yesnoundefined par = yesnoundefined.undefined;
                        if (AlwaysStartExternalProgram == -1)
                            par = yesnoundefined.yes;
                        if (AlwaysStartExternalProgram == 0)
                            par = yesnoundefined.no;
                        res = new lib.YesNoCheckResult(par, true);
                    }
                    if ((vok.getURI() != null && i == 1) || res.res == yesnoundefined.no) {
                        Intent intent = new Intent(this, AdvFileChooser.class);
                        ArrayList<String> extensions = new ArrayList<>();
                        extensions.add(".k??");
                        extensions.add(".v??");
                        extensions.add(".K??");
                        extensions.add(".V??");
                        extensions.add(".KAR");
                        extensions.add(".VOK");
                        extensions.add(".kar");
                        extensions.add(".vok");
                        extensions.add(".dic");
                        extensions.add(".DIC");

                        if (libString.IsNullOrEmpty(vok.getFileName())) {
                            if (vok.getURI() != null) {
                                intent.setData(vok.getURI());
                            }
                        } else {
                            File F = new File(vok.getFileName());
                            Uri uri = Uri.fromFile(F);
                            intent.setData(uri);
                        }
                        intent.putExtra("URIName", vok.getURIName());
                        intent.putStringArrayListExtra("filterFileExtension", extensions);
                        intent.putExtra("blnUniCode", blnUniCode);
                        intent.putExtra("DefaultDir", new File(JMGDataDirectory).exists() ? JMGDataDirectory
                                : Environment.getExternalStorageDirectory().getPath());
                        intent.putExtra("selectFolder", false);
                        intent.putExtra("blnNew", blnNew);
                        if (_blnUniCode)
                            _oldUniCode = yesnoundefined.yes;
                        else
                            _oldUniCode = yesnoundefined.no;
                        _blnUniCode = blnUniCode;

                        this.startActivityForResult(intent, FILE_CHOOSERADV);
                        blnSuccess = true;
                    } else if (Build.VERSION.SDK_INT < 19) {
                        //org.openintents.filemanager
                        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                        intent.setData(vok.getURI());
                        intent.putExtra("org.openintents.extra.WRITEABLE_ONLY", true);
                        intent.putExtra("org.openintents.extra.TITLE", getString(R.string.SaveAs));
                        intent.putExtra("org.openintents.extra.BUTTON_TEXT", getString(R.string.btnSave));
                        intent.setType("*/*");
                        Intent chooser = Intent.createChooser(intent, getString(R.string.SaveAs));
                        if (intent.resolveActivity(context.getPackageManager()) != null) {
                            startActivityForResult(chooser, FILE_OPENINTENT);
                            blnSuccess = true;
                        } else {
                            lib.ShowToast(this, getString(R.string.InstallFilemanager));
                            intent.setData(null);
                            intent.removeExtra("org.openintents.extra.WRITEABLE_ONLY");
                            intent.removeExtra("org.openintents.extra.TITLE");
                            intent.removeExtra("org.openintents.extra.BUTTON_TEXT");

                            startActivityForResult(chooser, FILE_OPENINTENT);
                            blnSuccess = true;
                        }

                    } else {
                        blnActionCreateDocument = true;
                        blnSuccess = true;
                    }
                } catch (Exception ex) {
                    blnSuccess = false;
                    Log.e("SaveAs", ex.getMessage(), ex);
                    if (i == 1) {
                        lib.ShowException(this, ex);
                    }
                }

                if (blnSuccess)
                    break;
            }

        } else if (Build.VERSION.SDK_INT >= 19) {
            blnActionCreateDocument = true;
        }
        if (blnActionCreateDocument) {
            /**
             * Open a file for writing and append some text to it.
             */
            // ACTION_OPEN_DOCUMENT is the intent to choose a file via the system's
            // file browser.
            Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
            // Create a file with the requested MIME type.
            String defaultURI = prefs.getString("defaultURI", "");
            if (!libString.IsNullOrEmpty(defaultURI)) {
                String FName = "";
                if (vok.getURI() != null) {
                    String path2 = lib.dumpUriMetaData(this, vok.getURI());
                    if (path2.contains(":"))
                        path2 = path2.split(":")[0];
                    FName = path2.substring(path2.lastIndexOf("/") + 1);
                } else if (!libString.IsNullOrEmpty(vok.getFileName())) {
                    FName = new File(vok.getFileName()).getName();
                }
                intent.putExtra(Intent.EXTRA_TITLE, FName);
                //defaultURI = (!defaultURI.endsWith("/")?defaultURI.substring(0,defaultURI.lastIndexOf("/")+1):defaultURI);
                Uri def = Uri.parse(defaultURI);
                intent.setData(def);
            } else {
                Log.d("empty", "empty");
                //intent.setType("file/*");
            }

            // Filter to only show results that can be "opened", such as a
            // file (as opposed to a list of contacts or timezones).
            intent.addCategory(Intent.CATEGORY_OPENABLE);

            // Filter to show only text files.
            intent.setType("*/*");

            startActivityForResult(intent, EDIT_REQUEST_CODE);

        }
    } catch (Exception ex) {
        libLearn.gStatus = "SaveVokAs";
        lib.ShowException(this, ex);
    }
}

From source file:im.vector.activity.RoomActivity.java

/**
 *
 * @param message/*ww w .j  a  va  2  s  .c  om*/
 * @param mediaUrl
 * @param mediaMimeType
 */
public void createDocument(Message message, final String mediaUrl, final String mediaMimeType) {
    String filename = "MatrixConsole_" + System.currentTimeMillis();

    MimeTypeMap mime = MimeTypeMap.getSingleton();
    filename += "." + mime.getExtensionFromMimeType(mediaMimeType);

    if (message instanceof FileMessage) {
        FileMessage fileMessage = (FileMessage) message;

        if (null != fileMessage.body) {
            filename = fileMessage.body;
        }
    }

    mPendingMediaUrl = mediaUrl;
    mPendingMimeType = mediaMimeType;

    Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT).addCategory(Intent.CATEGORY_OPENABLE)
            .setType(mediaMimeType).putExtra(Intent.EXTRA_TITLE, filename);

    startActivityForResult(intent, CREATE_DOCUMENT);

}