Refine search
@Override public void onReceive(Context context, Intent intent) { final Uri data = intent.getData(); if (data == null) { Log.e(EmulatorDebug.LOG_TAG, "termux-open: Called without intent data"); final String contentTypeExtra = intent.getStringExtra("content-type"); final boolean useChooser = intent.getBooleanExtra("chooser", false); final String intentAction = intent.getAction() == null ? Intent.ACTION_VIEW : intent.getAction(); switch (intentAction) { int lastDotIndex = fileName.lastIndexOf('.'); String fileExtension = fileName.substring(lastDotIndex + 1); MimeTypeMap mimeTypes = MimeTypeMap.getSingleton(); contentTypeToUse = mimeTypes.getMimeTypeFromExtension(fileExtension.toLowerCase()); if (contentTypeToUse == null) contentTypeToUse = "application/octet-stream"; } else {
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension( MimeTypeMap.getFileExtensionFromUrl(filename)); if (TextUtils.isEmpty(mimeType)) { mimeType = "image/jpeg"; .build(); Intent intent = new Intent(); intent.setAction(Intent.ACTION_SEND); intent.putExtra(Intent.EXTRA_STREAM, uri); intent.setType(mimeType);
public boolean checkType(ContentResolver resolver, Uri uri) { MimeTypeMap map = MimeTypeMap.getSingleton(); if (uri == null) { return false; } String type = map.getExtensionFromMimeType(resolver.getType(uri)); String path = null; // lazy load the path and prevent resolve for multiple times boolean pathParsed = false; for (String extension : mExtensions) { if (extension.equals(type)) { return true; } if (!pathParsed) { // we only resolve the path for one time path = PhotoMetadataUtils.getPath(resolver, uri); if (!TextUtils.isEmpty(path)) { path = path.toLowerCase(Locale.US); } pathParsed = true; } if (path != null && path.endsWith(extension)) { return true; } } return false; } }
private boolean isVideoFileUri(String uri) { String extension = MimeTypeMap.getFileExtensionFromUrl(uri); String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension); return mimeType != null && mimeType.startsWith("video/"); } }
public static String getMimeType(String url) { String extension = getExtension(url); if (!MimeTypeMap.getSingleton().hasExtension(extension)) return ""; String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension); return TextUtils.isEmpty(mimeType) ? "" : mimeType; }
public static String getMimeType(final String fileName) { String result = "application/octet-stream"; int extPos = fileName.lastIndexOf("."); if (extPos != -1) { String ext = fileName.substring(extPos + 1); result = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext); } return result; } }
MimeTypeMap mime = MimeTypeMap.getSingleton(); String ext = newFile.getName().substring(newFile.getName().lastIndexOf(".") + 1); String type = mime.getMimeTypeFromExtension(ext); try { Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); Uri contentUri = FileProvider.getUriForFile(getContext(), "com.your.package.fileProvider", newFile); intent.setDataAndType(contentUri, type); } else { intent.setDataAndType(Uri.fromFile(newFile), type); } startActivityForResult(intent, ACTIVITY_VIEW_ATTACHMENT); } catch (ActivityNotFoundException anfe) { Toast.makeText(getContext(), "No activity found to open this attachment.", Toast.LENGTH_LONG).show(); }
private boolean viewWithFileProvider(@NonNull File file) { if (!file.exists()) return true; String ext = Utility.getFileExt(file.getName()); if (ext == null) return false; String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext.substring(1)); Log.v(TAG, "Mime: " + mimeType + " package: " + BuildConfig.APPLICATION_ID + ".provider"); Uri uri = FileProvider.getUriForFile(mContext, BuildConfig.APPLICATION_ID + ".provider", file); Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setDataAndType(uri, mimeType); intent.addFlags(FLAG_GRANT_READ_URI_PERMISSION); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { intent.addFlags(FLAG_GRANT_PREFIX_URI_PERMISSION); } if(Build.VERSION.SDK_INT <= Build.VERSION_CODES.M) { intent.addFlags(FLAG_ACTIVITY_NEW_TASK); } //mContext.grantUriPermission(packageName, uri, Intent.FLAG_GRANT_READ_URI_PERMISSION); Log.v(TAG, "Starting intent: " + intent); if (intent.resolveActivity(mContext.getPackageManager()) != null) { mContext.startActivity(intent); } else { Toast noPlayerToast = Toast.makeText(mContext, R.string.toast_no_player, Toast.LENGTH_LONG); noPlayerToast.show(); } return true; }
public void askOpenBackup() { Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.addCategory(Intent.CATEGORY_OPENABLE); intent.setType(MimeTypeMap.getSingleton().getMimeTypeFromExtension("zip")); startActivityForResult(intent, BACKUP_FILE_REQUEST_CODE); setSlideAnimation(false); }
private static void openFile(Context context, File aFile) { Intent myIntent = new Intent(Intent.ACTION_VIEW); File file = new File(aFile.getAbsolutePath()); String extension = MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(file).toString()); String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension); myIntent.setDataAndType(Uri.fromFile(file), mimeType); context.startActivity(myIntent); }
String absolutepath = MyApplication.appRootDir + relpath;//change for your application Intent intent = new Intent(Intent.ACTION_ATTACH_DATA); MimeTypeMap map = MimeTypeMap.getSingleton(); String ext = absolutepath.substring(absolutepath.lastIndexOf('.') + 1); String mimeType = map.getMimeTypeFromExtension(ext); Uri uri = Uri.fromFile(new File(absolutepath)); intent.setDataAndType(uri, mimeType); intent.putExtra("mimeType", mimeType); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); Activity activity = (Activity) this; activity.startActivity(Intent.createChooser( intent, activity.getString(R.string.set_as)));
String url = "https://twitter.com"; MimeTypeMap map = MimeTypeMap.getSingleton(); String type = map.getMimeTypeFromExtension(url); Intent i = new Intent(Intent.ACTION_VIEW); i.setType(type); i.setData(Uri.parse(url)); ComponentName comp = new ComponentName("com.android.browser", "com.android.browser.BrowserActivity"); i.setComponent(comp); i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
MimeTypeMap myMime = MimeTypeMap.getSingleton(); Intent newIntent = new Intent(Intent.ACTION_VIEW); String mimeType = myMime.getMimeTypeFromExtension(fileExt(getFile()).substring(1)); newIntent.setDataAndType(Uri.fromFile(getFile()),mimeType); newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); try { context.startActivity(newIntent); } catch (ActivityNotFoundException e) { Toast.makeText(context, "No handler for this type of file.", Toast.LENGTH_LONG).show(); }
File file = new File(filePath); MimeTypeMap map = MimeTypeMap.getSingleton(); String ext = MimeTypeMap.getFileExtensionFromUrl(file.getName()); String type = map.getMimeTypeFromExtension(ext); if (type == null) type = "*/*"; Intent intent = new Intent(Intent.ACTION_VIEW); Uri data = Uri.fromFile(file); intent.setDataAndType(data, type); startActivity(intent);
Uri uri = Uri.fromFile(entry); Intent intent = new Intent(android.content.Intent.ACTION_VIEW); String mime = "*/*"; MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton(); if (mimeTypeMap.hasExtension( mimeTypeMap.getFileExtensionFromUrl(uri.toString()))) mime = mimeTypeMap.getMimeTypeFromExtension( mimeTypeMap.getFileExtensionFromUrl(uri.toString())); intent.setDataAndType(uri,mime); startActivity(intent);
/** * To find out the extension of required object in given uri * Solution by http://stackoverflow.com/a/36514823/1171484 */ public static String getMimeType(Activity context, Uri uri) { String extension; //Check uri format to avoid null if (ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) { //If scheme is a content extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(context.getContentResolver().getType(uri)); if (TextUtils.isEmpty(extension)) { extension = MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(new File(uri.getPath())).toString()); } } else { //If scheme is a File //This will replace white spaces with %20 and also other special characters. This will avoid returning null values on file // name with spaces and special characters. extension = MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(new File(uri.getPath())).toString()); if (TextUtils.isEmpty(extension)) { extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(context.getContentResolver().getType(uri)); } } if (TextUtils.isEmpty(extension)) { extension = getMimeTypeByFileName(TUriParse.getFileWithUri(uri, context).getName()); } return extension; }
@Test public void addingMappingShouldWorkCorrectly() { ShadowMimeTypeMap shadowMimeTypeMap = Shadows.shadowOf(MimeTypeMap.getSingleton()); shadowMimeTypeMap.addExtensionMimeTypMapping(VIDEO_EXTENSION, VIDEO_MIMETYPE); shadowMimeTypeMap.addExtensionMimeTypMapping(IMAGE_EXTENSION, IMAGE_MIMETYPE); assertTrue(MimeTypeMap.getSingleton().hasExtension(VIDEO_EXTENSION)); assertTrue(MimeTypeMap.getSingleton().hasExtension(IMAGE_EXTENSION)); assertTrue(MimeTypeMap.getSingleton().hasMimeType(VIDEO_MIMETYPE)); assertTrue(MimeTypeMap.getSingleton().hasMimeType(IMAGE_MIMETYPE)); assertEquals(IMAGE_EXTENSION, MimeTypeMap.getSingleton().getExtensionFromMimeType(IMAGE_MIMETYPE)); assertEquals(VIDEO_EXTENSION, MimeTypeMap.getSingleton().getExtensionFromMimeType(VIDEO_MIMETYPE)); assertEquals(IMAGE_MIMETYPE, MimeTypeMap.getSingleton().getMimeTypeFromExtension(IMAGE_EXTENSION)); assertEquals(VIDEO_MIMETYPE, MimeTypeMap.getSingleton().getMimeTypeFromExtension(VIDEO_EXTENSION)); }
private static String getFileName(String mimeType, String displayName) { String extension = MimeTypeMap.getFileExtensionFromUrl(displayName).toLowerCase(); if ((extension == null) || !Objects.equals(mimeType, MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension))) { extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType); if (extension != null) { displayName = displayName + "." + extension; } } return displayName; }
/** * Get file name from uri. */ private String getDisplayNameForUri(Uri uri) { String displayName; if (!ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) { displayName = uri.getLastPathSegment(); } else { displayName = getDisplayNameFromContentResolver(uri); if (displayName == null) { // last chance to have a name displayName = uri.getLastPathSegment().replaceAll("\\s", ""); } // Add best possible extension int index = displayName.lastIndexOf("."); if (index == -1 || MimeTypeMap.getSingleton() .getMimeTypeFromExtension(displayName.substring(index + 1)) == null) { String mimeType = this.getContentResolver().getType(uri); String extension = MimeTypeMap.getSingleton() .getExtensionFromMimeType(mimeType); if (extension != null) { displayName += "." + extension; } } } // Replace path separator characters to avoid inconsistent paths return displayName != null ? displayName.replaceAll("/", "-") : null; }
@Test public void byDefaultThereShouldBeNoMapping() { assertFalse(MimeTypeMap.getSingleton().hasExtension(VIDEO_EXTENSION)); assertFalse(MimeTypeMap.getSingleton().hasExtension(IMAGE_EXTENSION)); }