List of usage examples for android.webkit MimeTypeMap getSingleton
public static MimeTypeMap getSingleton()
From source file:edu.cmu.cylab.starslinger.view.ComposeFragment.java
@SuppressWarnings("deprecation") private void drawFileImage() { String filenameArray[] = mFilePath.split("\\."); String extension = filenameArray[filenameArray.length - 1]; String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension); if (mThumb != null && mThumb.length > 0) { ByteArrayInputStream in = new ByteArrayInputStream(mThumb); BitmapDrawable tn = new BitmapDrawable(in); mImageViewFile.setImageDrawable(tn); } else {/*from ww w .j a v a2s .c o m*/ Intent viewIntent = new Intent(Intent.ACTION_VIEW); viewIntent.setType(mime); PackageManager pm = getActivity().getPackageManager(); List<ResolveInfo> lract = pm.queryIntentActivities(viewIntent, PackageManager.MATCH_DEFAULT_ONLY); boolean resolved = false; for (ResolveInfo ri : lract) { if (!resolved) { try { Drawable icon = pm.getApplicationIcon(ri.activityInfo.packageName); mImageViewFile.setImageDrawable(icon); resolved = true; } catch (NameNotFoundException e) { mImageViewFile.setImageDrawable(getResources().getDrawable(R.drawable.ic_menu_file)); } } } } }
From source file:com.sentaroh.android.TextFileBrowser.MainActivity.java
private void addFileToViewedFileList(boolean set_selection, String fp) { if (mGlblParms.debugEnabled) Log.v(APPLICATION_TAG, "addFileToViewedFileList, fp=" + fp); ViewedFileListItem vfli = new ViewedFileListItem(); vfli.file_path = fp;/*w w w.j a va2 s . c o m*/ vfli.file_name = fp.substring(fp.lastIndexOf("/") + 1); String ft = ""; if (vfli.file_name.lastIndexOf(".") >= 0) ft = vfli.file_name.substring(vfli.file_name.lastIndexOf(".") + 1); String mt = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ft); if (mt != null && mt.startsWith("text")) vfli.browseMode = FileViewerAdapter.TEXT_BROWSER_BROWSE_MODE_CHAR; else vfli.browseMode = FileViewerAdapter.TEXT_BROWSER_BROWSE_MODE_HEX; vfli.tc_view = new ThreadCtrl(); mGlblParms.viewedFileList.add(vfli); vfli.ix_reader_view = new IndexedFileReader(mGlblParms.debugEnabled, mGlblParms.settingAlwayDeterminCharCode, this, mCommonDlg, vfli.tc_view, mGlblParms.settingEncodeName, mGlblParms.settingIndexCache, mGlblParms.settingBufferCharIndexSize, mGlblParms.settingBufferHexIndexSize, mGlblParms.settingBufferPoolSize, this); mViewedFileListAdapter.notifyDataSetChanged(); vfli.file_view_fragment = FileViewerFragment.newInstance(fp); if (set_selection) mViewedFileListSpinner.setSelection(mGlblParms.viewedFileList.size() - 1); }
From source file:eu.alefzero.owncloud.ui.fragment.FileDetailFragment.java
/** * Updates the view with all relevant details about that file. *///from www . j av a 2 s. c om public void updateFileDetails() { if (mFile != null && mAccount != null && mLayout == R.layout.file_details_fragment) { Button downloadButton = (Button) getView().findViewById(R.id.fdDownloadBtn); // set file details setFilename(mFile.getFileName()); setFiletype(DisplayUtils.convertMIMEtoPrettyPrint(mFile.getMimetype())); setFilesize(mFile.getFileLength()); if (ocVersionSupportsTimeCreated()) { setTimeCreated(mFile.getCreationTimestamp()); } setTimeModified(mFile.getModificationTimestamp()); CheckBox cb = (CheckBox) getView().findViewById(R.id.fdKeepInSync); cb.setChecked(mFile.keepInSync()); if (mFile.getStoragePath() != null) { // Update preview if (mFile.getMimetype().startsWith("image/")) { BitmapLoader bl = new BitmapLoader(); bl.execute(new String[] { mFile.getStoragePath() }); } // Change download button to open button downloadButton.setText(R.string.filedetails_open); downloadButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String storagePath = mFile.getStoragePath(); String encodedStoragePath = WebdavUtils.encodePath(storagePath); try { Intent i = new Intent(Intent.ACTION_VIEW); i.setDataAndType(Uri.parse("file://" + encodedStoragePath), mFile.getMimetype()); i.setFlags( Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); startActivity(i); } catch (Throwable t) { Log.e(TAG, "Fail when trying to open with the mimeType provided from the ownCloud server: " + mFile.getMimetype()); boolean toastIt = true; String mimeType = ""; try { Intent i = new Intent(Intent.ACTION_VIEW); mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension( storagePath.substring(storagePath.lastIndexOf('.') + 1)); if (mimeType != null && !mimeType.equals(mFile.getMimetype())) { i.setDataAndType(Uri.parse("file://" + encodedStoragePath), mimeType); i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); startActivity(i); toastIt = false; } } catch (IndexOutOfBoundsException e) { Log.e(TAG, "Trying to find out MIME type of a file without extension: " + storagePath); } catch (ActivityNotFoundException e) { Log.e(TAG, "No activity found to handle: " + storagePath + " with MIME type " + mimeType + " obtained from extension"); } catch (Throwable th) { Log.e(TAG, "Unexpected problem when opening: " + storagePath, th); } finally { if (toastIt) { Toast.makeText(getActivity(), "There is no application to handle file " + mFile.getFileName(), Toast.LENGTH_SHORT).show(); } } } } }); } else { // Make download button effective downloadButton.setOnClickListener(this); } } }
From source file:org.kontalk.util.MediaStorage.java
/** Guesses the MIME type of an {@link Uri}. */ public static String getType(Context context, Uri uri) { // try Android detection String mime = context.getContentResolver().getType(uri); // the following methods actually use the same underlying implementation // (libcore.net.MimeUtils), but that could change in the future so no // hurt in trying them all just in case. // Lowercasing the filename seems to help in detecting the correct MIME. if (mime == null) // try WebKit detection mime = MimeTypeMap.getSingleton() .getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(uri.toString()).toLowerCase()); if (mime == null) // try Java detection mime = URLConnection.guessContentTypeFromName(uri.toString().toLowerCase()); return mime;//from w w w. j av a2s . c o m }
From source file:ir.rasen.charsoo.controller.image_loader.core.download.BaseImageDownloader.java
private boolean isVideoFileUri(String uri) { String extension = MimeTypeMap.getFileExtensionFromUrl(uri); String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension); return mimeType != null && mimeType.startsWith("video/"); }
From source file:org.zywx.wbpalmstar.engine.EUtil.java
public static void installApp(Context context, String inAppPath) { if (null == inAppPath || 0 == inAppPath.trim().length()) { return;// ww w . j a v a2 s.c o m } String reallyPath = ""; File file = new File(inAppPath); if (file.exists()) { reallyPath = inAppPath; } else { reallyPath = copyFileToStorage(context, inAppPath); if (null == reallyPath) { return; } } // install apk. Intent intent = new Intent(Intent.ACTION_VIEW); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); MimeTypeMap type = MimeTypeMap.getSingleton(); String mime = type.getMimeTypeFromExtension("apk"); reallyPath = reallyPath.contains("file://") ? reallyPath : ("file://" + reallyPath); intent.setDataAndType(Uri.parse(reallyPath), mime); context.startActivity(intent); }
From source file:com.hippo.nimingban.ui.GalleryActivity2.java
private static boolean addImageExtension(UniFile uniFile) { String filename = uniFile.getName(); String extension = FileUtils.getExtensionFromFilename(filename); if (extension != null) { return true; }// w ww .j a v a 2 s. c o m // Get extexsin InputStream is = null; try { is = uniFile.openInputStream(); BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeStream(is, null, options); extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(options.outMimeType); } catch (IOException e) { return false; } finally { IOUtils.closeQuietly(is); } String newFilename = filename + "." + extension; // Remove old file UniFile parent = uniFile.getParentFile(); if (parent != null) { UniFile oldFile = parent.findFile(newFilename); if (oldFile != null) { oldFile.delete(); } } // Rename return uniFile.renameTo(newFilename); }
From source file:com.owncloud.android.ui.fragment.FileDetailFragment.java
@Override public void onClick(View v) { switch (v.getId()) { case R.id.fdDownloadBtn: { FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder(); FileUploaderBinder uploaderBinder = mContainerActivity.getFileUploaderBinder(); if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, mFile)) { downloaderBinder.cancel(mAccount, mFile); if (mFile.isDown()) { setButtonsForDown();/* ww w . j av a2 s . co m*/ } else { setButtonsForRemote(); } } else if (uploaderBinder != null && uploaderBinder.isUploading(mAccount, mFile)) { uploaderBinder.cancel(mAccount, mFile); if (!mFile.fileExists()) { // TODO make something better if (getActivity() instanceof FileDisplayActivity) { // double pane FragmentTransaction transaction = getActivity().getSupportFragmentManager() .beginTransaction(); transaction.replace(R.id.file_details_container, new FileDetailFragment(null, null), FTAG); // empty FileDetailFragment transaction.commit(); mContainerActivity.onFileStateChanged(); } else { getActivity().finish(); } } else if (mFile.isDown()) { setButtonsForDown(); } else { setButtonsForRemote(); } } else { mLastRemoteOperation = new SynchronizeFileOperation(mFile, null, mStorageManager, mAccount, true, false, getActivity()); WebdavClient wc = OwnCloudClientUtils.createOwnCloudClient(mAccount, getSherlockActivity().getApplicationContext()); mLastRemoteOperation.execute(wc, this, mHandler); // update ui boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity; getActivity().showDialog((inDisplayActivity) ? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT); setButtonsForTransferring(); // disable button immediately, although the synchronization does not result in a file transference } break; } case R.id.fdKeepInSync: { CheckBox cb = (CheckBox) getView().findViewById(R.id.fdKeepInSync); mFile.setKeepInSync(cb.isChecked()); mStorageManager.saveFile(mFile); /// register the OCFile instance in the observer service to monitor local updates; /// if necessary, the file is download Intent intent = new Intent(getActivity().getApplicationContext(), FileObserverService.class); intent.putExtra(FileObserverService.KEY_FILE_CMD, (cb.isChecked() ? FileObserverService.CMD_ADD_OBSERVED_FILE : FileObserverService.CMD_DEL_OBSERVED_FILE)); intent.putExtra(FileObserverService.KEY_CMD_ARG_FILE, mFile); intent.putExtra(FileObserverService.KEY_CMD_ARG_ACCOUNT, mAccount); Log.e(TAG, "starting observer service"); getActivity().startService(intent); if (mFile.keepInSync()) { onClick(getView().findViewById(R.id.fdDownloadBtn)); // force an immediate synchronization } break; } case R.id.fdRenameBtn: { EditNameDialog dialog = EditNameDialog.newInstance(getString(R.string.rename_dialog_title), mFile.getFileName(), this); dialog.show(getFragmentManager(), "nameeditdialog"); break; } case R.id.fdRemoveBtn: { ConfirmationDialogFragment confDialog = ConfirmationDialogFragment.newInstance( R.string.confirmation_remove_alert, new String[] { mFile.getFileName() }, mFile.isDown() ? R.string.confirmation_remove_remote_and_local : R.string.confirmation_remove_remote, mFile.isDown() ? R.string.confirmation_remove_local : -1, R.string.common_cancel); confDialog.setOnConfirmationListener(this); mCurrentDialog = confDialog; mCurrentDialog.show(getFragmentManager(), FTAG_CONFIRMATION); break; } case R.id.fdOpenBtn: { String storagePath = mFile.getStoragePath(); String encodedStoragePath = WebdavUtils.encodePath(storagePath); try { Intent i = new Intent(Intent.ACTION_VIEW); i.setDataAndType(Uri.parse("file://" + encodedStoragePath), mFile.getMimetype()); i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); startActivity(i); } catch (Throwable t) { Log.e(TAG, "Fail when trying to open with the mimeType provided from the ownCloud server: " + mFile.getMimetype()); boolean toastIt = true; String mimeType = ""; try { Intent i = new Intent(Intent.ACTION_VIEW); mimeType = MimeTypeMap.getSingleton() .getMimeTypeFromExtension(storagePath.substring(storagePath.lastIndexOf('.') + 1)); if (mimeType == null || !mimeType.equals(mFile.getMimetype())) { if (mimeType != null) { i.setDataAndType(Uri.parse("file://" + encodedStoragePath), mimeType); } else { // desperate try i.setDataAndType(Uri.parse("file://" + encodedStoragePath), "*/*"); } i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); startActivity(i); toastIt = false; } } catch (IndexOutOfBoundsException e) { Log.e(TAG, "Trying to find out MIME type of a file without extension: " + storagePath); } catch (ActivityNotFoundException e) { Log.e(TAG, "No activity found to handle: " + storagePath + " with MIME type " + mimeType + " obtained from extension"); } catch (Throwable th) { Log.e(TAG, "Unexpected problem when opening: " + storagePath, th); } finally { if (toastIt) { Toast.makeText(getActivity(), "There is no application to handle file " + mFile.getFileName(), Toast.LENGTH_SHORT) .show(); } } } break; } default: Log.e(TAG, "Incorrect view clicked!"); } /* else if (v.getId() == R.id.fdShareBtn) { Thread t = new Thread(new ShareRunnable(mFile.getRemotePath())); t.start(); }*/ }
From source file:com.dnielfe.manager.preview.MimeTypes.java
@Nullable public static String getMimeType(File file) { if (file.isDirectory()) { return null; }/* w ww . j a v a2 s. com*/ String type = null; final String extension = FilenameUtils.getExtension(file.getName()); if (extension != null && !extension.isEmpty()) { final String extensionLowerCase = extension.toLowerCase(Locale.getDefault()); final MimeTypeMap mime = MimeTypeMap.getSingleton(); type = mime.getMimeTypeFromExtension(extensionLowerCase); if (type == null) { type = MIME_TYPES.get(extensionLowerCase); } } return type; }