List of usage examples for android.webkit MimeTypeMap getFileExtensionFromUrl
public static String getFileExtensionFromUrl(String url)
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 a2 s. c om }
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:no.nordicsemi.android.nrftoolbox.dfu.DfuActivity.java
@Override protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) { if (resultCode != RESULT_OK) return;/*w w w. j a v a2 s. c o m*/ switch (requestCode) { case SELECT_FILE_REQ: // clear previous data mFilePath = null; mFileStreamUri = null; // and read new one final Uri uri = data.getData(); /* * The URI returned from application may be in 'file' or 'content' schema. * 'File' schema allows us to create a File object and read details from if directly. * * Data from 'Content' schema must be read by Content Provider. To do that we are using a Loader. */ if (uri.getScheme().equals("file")) { // the direct path to the file has been returned final String path = uri.getPath(); final File file = new File(path); mFilePath = path; mFileNameView.setText(file.getName()); mFileSizeView.setText(getString(R.string.dfu_file_size_text, file.length())); final boolean isHexFile = mStatusOk = MimeTypeMap.getFileExtensionFromUrl(path) .equalsIgnoreCase("HEX"); mFileStatusView.setText(isHexFile ? R.string.dfu_file_status_ok : R.string.dfu_file_status_invalid); mUploadButton.setEnabled(mSelectedDevice != null && isHexFile); } else if (uri.getScheme().equals("content")) { // an Uri has been returned mFileStreamUri = uri; // if application returned Uri for streaming, let's us it. Does it works? // FIXME both Uris works with Google Drive app. Why both? What's the difference? How about other apps like DropBox? final Bundle extras = data.getExtras(); if (extras != null && extras.containsKey(Intent.EXTRA_STREAM)) mFileStreamUri = extras.getParcelable(Intent.EXTRA_STREAM); // file name and size must be obtained from Content Provider final Bundle bundle = new Bundle(); bundle.putParcelable(EXTRA_URI, uri); getSupportLoaderManager().restartLoader(0, bundle, this); } break; default: break; } }
From source file:com.applozic.mobicommons.file.FileUtils.java
public static String getMimeType(String url) { String type = null;//w ww.j av a2 s . c o m String extension = MimeTypeMap.getFileExtensionFromUrl(url); if (TextUtils.isEmpty(extension) && url.contains(".")) { extension = url.substring(url.lastIndexOf('.') + 1).toLowerCase(); } if (!TextUtils.isEmpty(extension)) { MimeTypeMap mime = MimeTypeMap.getSingleton(); type = mime.getMimeTypeFromExtension(extension.toLowerCase()); } return type; }
From source file:no.nordicsemi.android.nrftoolbox.dfu.DfuActivity.java
@Override public void onLoadFinished(final Loader<Cursor> loader, final Cursor data) { if (data.moveToNext()) { /*//w w w . j ava 2s.c o m * Here we have to check the column indexes by name as we have requested for all. The order may be different. */ final String fileName = data .getString(data.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME)/* 0 DISPLAY_NAME */); final int fileSize = data.getInt(data.getColumnIndex(MediaStore.MediaColumns.SIZE) /* 1 SIZE */); String filePath = null; final int dataIndex = data.getColumnIndex(MediaStore.MediaColumns.DATA); if (dataIndex != -1) filePath = data.getString(dataIndex /*2 DATA */); if (!TextUtils.isEmpty(filePath)) mFilePath = filePath; mFileNameView.setText(fileName); mFileSizeView.setText(getString(R.string.dfu_file_size_text, fileSize)); final boolean isHexFile = mStatusOk = MimeTypeMap.getFileExtensionFromUrl(fileName) .equalsIgnoreCase("HEX"); mFileStatusView.setText(isHexFile ? R.string.dfu_file_status_ok : R.string.dfu_file_status_invalid); mUploadButton.setEnabled(mSelectedDevice != null && isHexFile); } }
From source file:org.deviceconnect.android.deviceplugin.chromecast.profile.ChromeCastMediaPlayerProfile.java
/** * mediaId?dummyUrl??/*from w w w .j a v a2s.com*/ * * @param mediaId ID * @return dummyUrl URL */ private String getDummyUrlFromMediaId(int mediaId) { Uri targetUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; String path = getPathFromUri(ContentUris.withAppendedId(targetUri, Long.valueOf(mediaId))); if (path == null) { return null; } ChromeCastHttpServer server = ((ChromeCastService) getContext()).getChromeCastHttpServer(); String dir = new File(path).getParent(); String realName = new File(path).getName(); String extension = MimeTypeMap.getFileExtensionFromUrl(realName); String dummyName = getMd5("" + System.currentTimeMillis()) + "." + extension; server.setFilePath(dir, realName, "/" + dummyName); return "http://" + getIpAddress() + ":" + server.getListeningPort() + "/" + dummyName; }
From source file:com.karura.framework.plugins.Capture.java
/** * Looks up the mime type of a given file name. * /*from w w w .j av a 2s . co m*/ * @param filename * @return a mime type */ @SuppressLint("DefaultLocale") public static String getMimeType(String filename) { if (filename != null) { // Stupid bug in getFileExtensionFromUrl when the file name has a space // So we need to replace the space with a url encoded %20 String url = filename.replace(" ", "%20").toLowerCase(); MimeTypeMap map = MimeTypeMap.getSingleton(); String extension = MimeTypeMap.getFileExtensionFromUrl(url); if (extension.toLowerCase().equals("3ga")) { return AUDIO_3GPP; } else { return map.getMimeTypeFromExtension(extension); } } else { return ""; } }
From source file:com.buddi.client.dfu.DfuActivity.java
/** * Updates the file information on UI/*from w ww . ja v a 2 s . c o m*/ * * @param fileName * file name * @param fileSize * file length */ private void updateFileInfo(final String fileName, final long fileSize, final int fileType) { mFileNameView.setText(fileName); switch (fileType) { case DfuService.TYPE_SOFT_DEVICE: mFileTypeView.setText(getResources().getStringArray(R.array.dfu_file_type)[0]); break; case DfuService.TYPE_BOOTLOADER: mFileTypeView.setText(getResources().getStringArray(R.array.dfu_file_type)[1]); break; case DfuService.TYPE_APPLICATION: mFileTypeView.setText(getResources().getStringArray(R.array.dfu_file_type)[2]); break; case DfuService.TYPE_AUTO: mFileTypeView.setText(getResources().getStringArray(R.array.dfu_file_type)[3]); break; } mFileSizeView.setText(getString(R.string.dfu_file_size_text, fileSize)); final String extension = mFileType == DfuService.TYPE_AUTO ? "ZIP" : "HEX"; final boolean isHexFile = mStatusOk = MimeTypeMap.getFileExtensionFromUrl(fileName) .equalsIgnoreCase(extension); mFileStatusView.setText(isHexFile ? R.string.dfu_file_status_ok : R.string.dfu_file_status_invalid); mUploadButton.setEnabled(mSelectedDevice != null && isHexFile); }
From source file:com.example.commander.CommandManager.java
/** * Start intent for file open//from w w w . ja v a 2 s. co m */ private void StartOpenFileIntent() { // Make intent File file = new File(mFilePath); MimeTypeMap map = MimeTypeMap.getSingleton(); String extension = MimeTypeMap.getFileExtensionFromUrl(file.getName()); String type = map.getMimeTypeFromExtension(extension); if (type == null) { type = "*/*"; } Intent intent = new Intent(Intent.ACTION_VIEW); Uri dataForIntent = Uri.fromFile(file); intent.setDataAndType(dataForIntent, type); mActivity.startActivity(intent); }
From source file:nya.miku.wishmaster.api.AbstractLynxChanModule.java
public String sendPost(SendPostModel model, ProgressListener listener, CancellableTask task) throws Exception { String url = getUsingUrl() + ".api/" + (model.threadNumber == null ? "newThread" : "replyThread"); JSONObject jsonPayload = new JSONObject(); JSONObject jsonParameters = new JSONObject(); jsonPayload.put("captchaId", lastCaptchaId); jsonParameters.put("name", model.name); jsonParameters.put("password", model.password); jsonParameters.put("subject", model.subject); jsonParameters.put("message", model.comment); jsonParameters.put("boardUri", model.boardName); jsonParameters.put("email", model.sage ? "sage" : model.email); if (model.threadNumber != null) jsonParameters.put("threadId", model.threadNumber); if (model.captchaAnswer != null && model.captchaAnswer.length() > 0) jsonParameters.put("captcha", model.captchaAnswer); if (model.icon > 0) jsonParameters.put("flag", flagsMap.get(model.boardName).get(model.icon - 1)); MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton(); if (model.attachments != null && model.attachments.length > 0) { JSONArray files = new JSONArray(); for (int i = 0; i < model.attachments.length; ++i) { String ext = MimeTypeMap.getFileExtensionFromUrl(model.attachments[i].toURI().toString()); String mime = mimeTypeMap.getMimeTypeFromExtension(ext); String md5 = checkFileIdentifier(model.attachments[i], mime, listener, task); JSONObject file = new JSONObject(); file.put("name", model.attachments[i].getName()); if (md5 != null) { file.put("md5", md5); file.put("mime", mime); } else { file.put("content", "data:" + mime + ";base64," + base64EncodeFile(model.attachments[i])); }/*from w w w .ja v a 2 s .c o m*/ file.put("spoiler", model.custommark); files.put(file); } jsonParameters.put("files", files); } jsonPayload.put("parameters", jsonParameters); JSONEntry payload = new JSONEntry(jsonPayload); HttpRequestModel request = HttpRequestModel.builder().setPOST(payload).setNoRedirect(true).build(); String response = HttpStreamer.getInstance().getStringFromUrl(url, request, httpClient, null, task, true); lastCaptchaId = null; JSONObject result = new JSONObject(response); String status = result.optString("status"); if ("ok".equals(status)) { UrlPageModel urlPageModel = new UrlPageModel(); urlPageModel.type = UrlPageModel.TYPE_THREADPAGE; urlPageModel.chanName = getChanName(); urlPageModel.boardName = model.boardName; urlPageModel.threadNumber = model.threadNumber; if (model.threadNumber == null) { urlPageModel.threadNumber = Integer.toString(result.optInt("data")); } else { urlPageModel.postNumber = Integer.toString(result.optInt("data")); } return buildUrl(urlPageModel); } else if (status.contains("error") || status.contains("blank")) { String errorMessage = result.optString("data"); if (errorMessage.length() > 0) { throw new Exception(errorMessage); } } throw new Exception("Unknown Error"); }