Example usage for android.webkit MimeTypeMap getMimeTypeFromExtension

List of usage examples for android.webkit MimeTypeMap getMimeTypeFromExtension

Introduction

In this page you can find the example usage for android.webkit MimeTypeMap getMimeTypeFromExtension.

Prototype

@Nullable
public String getMimeTypeFromExtension(String extension) 

Source Link

Document

Return the MIME type for the given extension.

Usage

From source file:com.phonegap.plugins.openfilewithexternalapp.OpenFileWithExternalApp.java

public PluginResult execute(String action, JSONArray args, String callbackId) {

    try {//from  ww w . j ava  2s .  c  o  m

        String filePath = args.getString(0);

        Intent intent = new Intent();

        intent.setAction(android.content.Intent.ACTION_VIEW);

        File fileToOpen = new File(filePath);

        MimeTypeMap mime = MimeTypeMap.getSingleton();

        String extension = fileToOpen.getName().substring(fileToOpen.getName().lastIndexOf(".") + 1)
                .toLowerCase();

        String type = mime.getMimeTypeFromExtension(extension);

        intent.setAction(Intent.ACTION_VIEW);

        Uri uri = Uri.fromFile(fileToOpen);

        intent.setDataAndType(uri, type);

        this.ctx.startActivity(intent);
    }

    catch (JSONException e) {

        e.printStackTrace();

    }

    PluginResult mPlugin = new PluginResult(PluginResult.Status.NO_RESULT);

    return mPlugin;
}

From source file:org.zywx.wbpalmstar.engine.EDownloadDialog.java

private void init(String inUrl) {
    url = inUrl;//  w  ww .  java 2 s. c  o  m
    String suffix = makeFileSuffix(url);
    MimeTypeMap mtm = MimeTypeMap.getSingleton();
    mimetype = mtm.getMimeTypeFromExtension(suffix);
    if (null == mimetype) {

    }
    setProgress(0);
    setIcon(EResources.icon);
    setCancelable(false);
    setTitle("");
    setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    setMax(100);
    setButton("?", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            mFromStop = true;
            stopDownload();
        }
    });
}

From source file:activity.DetailsActivity.java

/**
 * Opens the document if it is on storage, else downloads it before open it.
 * /*from  ww  w  . j  a v  a  2s  . c o  m*/
 * @param item
 *            the document to open
 */
public void openFileInMemory(final Document item) {
    mItem = item;
    MimeTypeMap map = MimeTypeMap.getSingleton();
    final String mime = map.getMimeTypeFromExtension(item.getExtension());
    mSubPath = item.getTitle() + "." + item.getExtension();

    if (mime != null) {
        if (item.isOnMemory() && Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
                || Environment.MEDIA_MOUNTED_READ_ONLY.equals(Environment.getExternalStorageState())) {
            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setDataAndType(Uri.fromFile(
                    new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
                            .getAbsolutePath() + "/" + mSubPath)),
                    mime.toLowerCase(Locale.US));

            startActivity(Intent.createChooser(i, getString(R.string.dialog_choose_app)));

        } else {
            getService().getDownloadTokenizedUrl(item.getList().getCours().getSysCode(),
                    item.getResourceString(), mDwlManagerHandler);
        }

    } else {
        DetailsActivity.this.getService().getDownloadTokenizedUrl(item.getList().getCours().getSysCode(),
                item.getResourceString(), mTokenizedURLHandler);
    }
}

From source file:com.phonegap.FileUtils.java

/**
 * Read content of text file and return as base64 encoded data url.
 * /*from   w ww.  j  ava2  s.c o  m*/
 * @param filename         The name of the file.
 * @return               Contents of file = data:<media type>;base64,<data>
 * @throws FileNotFoundException, IOException
 */
public String readAsDataURL(String filename) throws FileNotFoundException, IOException {
    byte[] bytes = new byte[1000];
    BufferedInputStream bis = new BufferedInputStream(getPathFromUri(filename), 1024);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    int numRead = 0;
    while ((numRead = bis.read(bytes, 0, 1000)) >= 0) {
        bos.write(bytes, 0, numRead);
    }

    // Determine content type from file name
    String contentType = null;
    if (filename.startsWith("content:")) {
        Uri fileUri = Uri.parse(filename);
        contentType = this.ctx.getContentResolver().getType(fileUri);
    } else {
        MimeTypeMap map = MimeTypeMap.getSingleton();
        contentType = map.getMimeTypeFromExtension(map.getFileExtensionFromUrl(filename));
    }

    byte[] base64 = Base64.encodeBase64(bos.toByteArray());
    String data = "data:" + contentType + ";base64," + new String(base64);
    return data;
}

From source file:com.todoroo.astrid.files.FilesControlSet.java

private void showFile(final TaskAttachment m) {
    final String fileType = m.containsNonNullValue(TaskAttachment.CONTENT_TYPE) ? m.getContentType()
            : TaskAttachment.FILE_TYPE_OTHER;
    final String filePath = m.getFilePath();

    if (fileType.startsWith(TaskAttachment.FILE_TYPE_AUDIO)) {
        play(m.getFilePath(), new PlaybackExceptionHandler() {
            @Override/* ww w .j a va 2 s .c o  m*/
            public void playbackFailed() {
                showFromIntent(filePath, fileType);
            }
        });
    } else if (fileType.startsWith(TaskAttachment.FILE_TYPE_IMAGE)) {
        activity.startActivity(new Intent(Intent.ACTION_VIEW) {
            {
                setDataAndType(Uri.fromFile(new File(filePath)), fileType);
            }
        });
    } else {
        String useType = fileType;
        if (fileType.equals(TaskAttachment.FILE_TYPE_OTHER)) {
            String extension = AndroidUtilities.getFileExtension(filePath);

            MimeTypeMap map = MimeTypeMap.getSingleton();
            String guessedType = map.getMimeTypeFromExtension(extension);
            if (!TextUtils.isEmpty(guessedType)) {
                useType = guessedType;
            }
            if (!useType.equals(guessedType)) {
                m.setContentType(useType);
                m.putTransitory(SyncFlags.ACTFM_SUPPRESS_OUTSTANDING_ENTRIES, true);
                taskAttachmentDao.saveExisting(m);
            }
        }
        showFromIntent(filePath, useType);
    }
}

From source file:com.doplgangr.secrecy.views.FileViewer.java

void decrypt(final EncryptedFile encryptedFile, final Runnable onFinish) {
    new AsyncTask<EncryptedFile, Void, File>() {
        @Override/*from ww  w.  j  a va  2 s .c om*/
        protected File doInBackground(EncryptedFile... encryptedFiles) {
            return getFile(encryptedFile, onFinish);
        }

        @Override
        protected void onPostExecute(File tempFile) {
            if (tempFile != null) {
                if (tempFile.getParentFile().equals(Storage.getTempFolder())) {
                    tempFile = new File(Storage.getTempFolder(), tempFile.getName());
                }
                Uri uri = OurFileProvider.getUriForFile(context, OurFileProvider.FILE_PROVIDER_AUTHORITY,
                        tempFile);
                MimeTypeMap myMime = MimeTypeMap.getSingleton();
                Intent newIntent = new Intent(android.content.Intent.ACTION_VIEW);
                String mimeType = myMime.getMimeTypeFromExtension(encryptedFile.getType());
                newIntent.setDataAndType(uri, mimeType);
                newIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                //altIntent: resort to using file provider when content provider does not work.
                Intent altIntent = new Intent(android.content.Intent.ACTION_VIEW);
                Uri rawuri = Uri.fromFile(tempFile);
                altIntent.setDataAndType(rawuri, mimeType);
                afterDecrypt(newIntent, altIntent);
            }
        }
    }.execute(encryptedFile);
}

From source file:mobisocial.bento.anyshare.ui.FeedItemListActivity.java

@Override
public void onActivityResult(int requestCode, int resultCode, Intent ret) {
    if (requestCode == REQUEST_PICK) {
        if (resultCode == RESULT_OK) {
            Log.d(TAG, ret.toString());// w  w  w  .  j  a  v  a 2 s. com
            Uri uri = ret.getData();
            Intent intent = new Intent(this, PostActivity.class);
            intent.setAction(Intent.ACTION_SEND);
            String mimetype = getContentResolver().getType(uri);
            String ext = MimeTypeMap.getFileExtensionFromUrl(uri.toString());

            MimeTypeMap mime = MimeTypeMap.getSingleton();
            if (mimetype == null || mimetype.isEmpty()) {
                if (!ext.isEmpty()) {
                    mimetype = mime.getMimeTypeFromExtension(ext);
                }
            }

            String fname = uri.getLastPathSegment();
            if (ext.isEmpty()) {
                fname += "." + mime.getExtensionFromMimeType(mimetype);
            }

            intent.setType(mimetype);
            intent.putExtra(Intent.EXTRA_SUBJECT, fname);
            intent.putExtra(Intent.EXTRA_TEXT, "");
            intent.putExtra(Intent.EXTRA_STREAM, uri);

            startActivityForResult(intent, HomeActivity.REQUEST_VIEW);

        }
    }
    super.onActivityResult(requestCode, resultCode, ret);
}

From source file:com.example.lista3new.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_layout);
    mFileDownloadedReceiver = new DownloadReceiver();
    LocalBroadcastManager.getInstance(this).registerReceiver(mFileDownloadedReceiver,
            new IntentFilter(SyncService.MESSAGE_UPDATE));
    mFileListView = (ListView) findViewById(R.id.fileList);
    mCurrentDirectoryInfo = (TextView) findViewById(R.id.currentPath);
    mLoginInfo = (TextView) findViewById(R.id.loginInfo);
    mFileListView.setOnItemClickListener(new OnItemClickListener() {

        @Override/*from w w  w.j av a2s .  c  o m*/
        public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
            File file = new File(currentPath + "/" + mFileListView.getItemAtPosition(position).toString());
            Log.d(TAG, "OnClick path: " + file.getPath());
            if (file.isDirectory()) {
                currentPath = file;
                loadFiles();
            } else {
                Intent intent = new Intent();
                intent.setAction(android.content.Intent.ACTION_VIEW);
                MimeTypeMap mime = MimeTypeMap.getSingleton();
                String ext = file.getName().substring(file.getName().indexOf(".") + 1);
                String type = mime.getMimeTypeFromExtension(ext);
                intent.setDataAndType(Uri.fromFile(file), type);
                startActivity(intent);
            }
        }
    });
    loadSettings();
    loadFiles();
    Log.i(TAG, "onCreate");
}

From source file:com.bitants.wally.fragments.ImageZoomFragment.java

private void setImageAsWallpaperPicker(Uri fileUri) {
    Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
    intent.setType("image/*");

    MimeTypeMap map = MimeTypeMap.getSingleton();
    String mimeType = map.getMimeTypeFromExtension("png");
    intent.setDataAndType(fileUri, mimeType);
    intent.putExtra("mimeType", mimeType);
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

    startActivity(Intent.createChooser(intent, getString(R.string.action_set_as)));
}

From source file:fileops.FileChooser.java

private void openFile(String file) {
    // TODO Auto-generated method stub
    File mfile = new File(file);
    MimeTypeMap myMime = MimeTypeMap.getSingleton();

    Intent newIntent = new Intent(android.content.Intent.ACTION_VIEW);

    // Intent newIntent = new Intent(Intent.ACTION_VIEW);
    String mimeType = myMime.getMimeTypeFromExtension(fileExt(mfile.toString()).substring(1));
    newIntent.setDataAndType(Uri.fromFile(mfile), mimeType);
    newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    try {//from www  . j  a  va  2 s . co m
        getActivity().startActivity(newIntent);
    } catch (android.content.ActivityNotFoundException e) {
        Toast.makeText(getActivity(), "No handler for this type of file.", 1000).show();
    }
}