List of usage examples for android.webkit MimeTypeMap getFileExtensionFromUrl
public static String getFileExtensionFromUrl(String url)
From source file:mobisocial.musubi.util.UriImage.java
private void initFromFile(Context context, Uri uri) { mPath = uri.getPath();//w w w . j a v a2 s. c o m MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton(); String extension = MimeTypeMap.getFileExtensionFromUrl(mPath); if (TextUtils.isEmpty(extension)) { // getMimeTypeFromExtension() doesn't handle spaces in filenames nor can it handle // urlEncoded strings. Let's try one last time at finding the extension. int dotPos = mPath.lastIndexOf('.'); if (0 <= dotPos) { extension = mPath.substring(dotPos + 1); } } mContentType = mimeTypeMap.getMimeTypeFromExtension(extension); // It's ok if mContentType is null. Eventually we'll show a toast telling the // user the picture couldn't be attached. }
From source file:com.android.xbrowser.FetchUrlMimeType.java
@Override public void run() { // User agent is likely to be null, though the AndroidHttpClient // seems ok with that. AndroidHttpClient client = AndroidHttpClient.newInstance(mUserAgent); HttpHost httpHost = Proxy.getPreferredHttpHost(mContext, mUri); if (httpHost != null) { ConnRouteParams.setDefaultProxy(client.getParams(), httpHost); }// w w w . j a v a 2s. c om HttpHead request = new HttpHead(mUri); if (mCookies != null && mCookies.length() > 0) { request.addHeader("Cookie", mCookies); } HttpResponse response; String mimeType = null; String contentDisposition = null; try { response = client.execute(request); // We could get a redirect here, but if we do lets let // the download manager take care of it, and thus trust that // the server sends the right mimetype if (response.getStatusLine().getStatusCode() == 200) { Header header = response.getFirstHeader("Content-Type"); if (header != null) { mimeType = header.getValue(); final int semicolonIndex = mimeType.indexOf(';'); if (semicolonIndex != -1) { mimeType = mimeType.substring(0, semicolonIndex); } } Header contentDispositionHeader = response.getFirstHeader("Content-Disposition"); if (contentDispositionHeader != null) { contentDisposition = contentDispositionHeader.getValue(); } } } catch (IllegalArgumentException ex) { request.abort(); } catch (IOException ex) { request.abort(); } finally { client.close(); } if (mimeType != null) { if (mimeType.equalsIgnoreCase("text/plain") || mimeType.equalsIgnoreCase("application/octet-stream")) { String newMimeType = MimeTypeMap.getSingleton() .getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(mUri)); if (newMimeType != null) { mRequest.setMimeType(newMimeType); } } String filename = URLUtil.guessFileName(mUri, contentDisposition, mimeType); mRequest.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, filename); } // Start the download DownloadManager manager = (DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE); manager.enqueue(mRequest); }
From source file:com.android.browser.kai.FetchUrlMimeType.java
@Override public void onPostExecute(String mimeType) { if (mimeType != null) { String url = mValues.getAsString(Downloads.COLUMN_URI); if (mimeType.equalsIgnoreCase("text/plain") || mimeType.equalsIgnoreCase("application/octet-stream")) { String newMimeType = MimeTypeMap.getSingleton() .getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(url)); if (newMimeType != null) { mValues.put(Downloads.COLUMN_MIME_TYPE, newMimeType); }/*from www.j av a 2 s . co m*/ } String filename = URLUtil.guessFileName(url, null, mimeType); mValues.put(Downloads.COLUMN_FILE_NAME_HINT, filename); } // Start the download final Uri contentUri = mActivity.getContentResolver().insert(Downloads.CONTENT_URI, mValues); mActivity.viewDownloads(contentUri); }
From source file:com.android.erowser.FetchUrlMimeType.java
@Override public void onPostExecute(String mimeType) { if (mimeType != null) { String url = mValues.getAsString(Downloads.COLUMN_URI); if (mimeType.equalsIgnoreCase("text/plain") || mimeType.equalsIgnoreCase("application/octet-stream")) { String newMimeType = MimeTypeMap.getSingleton() .getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(url)); if (newMimeType != null) { mValues.put(Downloads.COLUMN_MIME_TYPE, newMimeType); }//from w w w . j a v a 2s . co m } String filename = URLUtil.guessFileName(url, null, mimeType); mValues.put(Downloads.COLUMN_FILE_NAME_HINT, filename); } // Start the download //shuaiyuan removed ==> //final Uri contentUri = // mActivity.getContentResolver().insert(Downloads.CONTENT_URI, mValues); //mActivity.viewDownloads(contentUri); //shuaiyuan removed <== }
From source file:com.pdftron.pdf.utils.ViewerUtils.java
public static Map readImageIntent(Intent data, Context context, Uri outputFileUri) throws FileNotFoundException, Exception { final boolean isCamera; if (data == null || data.getData() == null) { isCamera = true;/*from w ww . j a v a2s. c o m*/ } else { final String action = data.getAction(); if (action == null) { isCamera = false; } else { isCamera = action.equals(MediaStore.ACTION_IMAGE_CAPTURE); } } Uri imageUri; if (isCamera) { imageUri = outputFileUri; AnalyticsHandlerAdapter.getInstance().sendEvent(AnalyticsHandlerAdapter.CATEGORY_FILEBROWSER, "Create new document from camera selected"); } else { imageUri = data.getData(); AnalyticsHandlerAdapter.getInstance().sendEvent(AnalyticsHandlerAdapter.CATEGORY_FILEBROWSER, "Create new document from local image file selected"); } String filePath; if (isCamera) { filePath = imageUri.getPath(); } else { filePath = Utils.getRealPathFromImageURI(context, imageUri); if (Utils.isNullOrEmpty(filePath)) { filePath = imageUri.getPath(); } } // try to get bitmap Bitmap bitmap = Utils.getBitmapFromImageUri(context, imageUri, filePath); // if a file is selected, check if it is an image file if (!isCamera) { // if type is null if (context.getContentResolver().getType(imageUri) == null) { String extension = MimeTypeMap.getFileExtensionFromUrl(imageUri.getPath()); final String[] extensions = { "jpeg", "jpg", "tiff", "tif", "gif", "png", "bmp" }; // if file extension is not an image extension if (!Arrays.asList(extensions).contains(extension) && extension != null && !extension.equals("")) { throw new FileNotFoundException("file extension is not an image extension"); } // if type is not an image } else if (!context.getContentResolver().getType(imageUri).startsWith("image/")) { throw new FileNotFoundException("type is not an image"); } } ////////////////// Determine if image needs to be rotated /////////////////// File imageFile = new File(imageUri.getPath()); ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath()); int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED); int imageRotation = 0; switch (orientation) { case ExifInterface.ORIENTATION_ROTATE_270: imageRotation = 270; break; case ExifInterface.ORIENTATION_ROTATE_180: imageRotation = 180; break; case ExifInterface.ORIENTATION_ROTATE_90: imageRotation = 90; break; } // in some devices (mainly Samsung), the EXIF is not saved with the image so look at the content // resolver as a second source of the image's rotation if (imageRotation == 0) { String[] orientationColumn = { MediaStore.Images.Media.ORIENTATION }; Cursor cur = context.getContentResolver().query(imageUri, orientationColumn, null, null, null); orientation = -1; if (cur != null && cur.moveToFirst()) { orientation = cur.getInt(cur.getColumnIndex(orientationColumn[0])); } if (orientation > 0) { imageRotation = orientation; } } if (imageRotation != 0) { Matrix matrix = new Matrix(); matrix.postRotate(imageRotation); bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); } Map<String, Object> output = new HashMap<>(); output.put("bitmap", bitmap); output.put("uri", imageUri); output.put("path", filePath); output.put("camera", isCamera); return output; }
From source file:net.sf.fdshare.BaseProvider.java
@SuppressLint("NewApi") @NonNull//from w w w . j a v a 2s.c om TimestampedMime guessTypeInternal(String filePath) { final TimestampedMime cachedResult = fileTypeCache.get(filePath); if (cachedResult != null && System.nanoTime() - cachedResult.when > 2_000_000_000) return cachedResult; final Set<String> types = new LinkedHashSet<>(); final TimestampedMime mime = new TimestampedMime(); final String actualExtension = MimeTypeMap.getFileExtensionFromUrl(filePath); if (!TextUtils.isEmpty(actualExtension)) { final String extType = map.getMimeTypeFromExtension(actualExtension); if (!TextUtils.isEmpty(extType) && !"application/octet-stream".equals(extType)) types.add(extType); final ContentType extType2 = ContentType.fromFileExtension(actualExtension); if (ContentType.OTHER != extType2) types.add(extType2.getMimeType()); } try (ParcelFileDescriptor newFd = openDescriptor(filePath, "r", false); FileInputStream fs = new FileInputStream(newFd.getFileDescriptor())) { mime.size = newFd.getStatSize(); final ContentInfo result; if (mime.size != 0 && (result = mimeLib.findMatch(fs)) != null) { final String detectedMime = result.getMimeType(); if (!TextUtils.isEmpty(detectedMime) && !"application/octet-stream".equals(detectedMime)) types.add(detectedMime); } } catch (IOException e) { e.printStackTrace(); } if (types.isEmpty()) types.add("application/octet-stream"); mime.mime = types.toArray(new String[types.size()]); mime.when = System.nanoTime(); fileTypeCache.put(filePath, mime); return mime; }
From source file:com.mogoweb.browser.FetchUrlMimeType.java
@Override public void run() { // User agent is likely to be null, though the AndroidHttpClient // seems ok with that. AndroidHttpClient client = AndroidHttpClient.newInstance(mUserAgent); // HttpHost httpHost; // try { // httpHost = Proxy.getPreferredHttpHost(mContext, mUri); // if (httpHost != null) { // ConnRouteParams.setDefaultProxy(client.getParams(), httpHost); // } // } catch (IllegalArgumentException ex) { // Log.e(LOGTAG,"Download failed: " + ex); // client.close(); // return; // }//from w w w. ja v a 2 s . c o m HttpHead request = new HttpHead(mUri); if (mCookies != null && mCookies.length() > 0) { request.addHeader("Cookie", mCookies); } HttpResponse response; String mimeType = null; String contentDisposition = null; try { response = client.execute(request); // We could get a redirect here, but if we do lets let // the download manager take care of it, and thus trust that // the server sends the right mimetype if (response.getStatusLine().getStatusCode() == 200) { Header header = response.getFirstHeader("Content-Type"); if (header != null) { mimeType = header.getValue(); final int semicolonIndex = mimeType.indexOf(';'); if (semicolonIndex != -1) { mimeType = mimeType.substring(0, semicolonIndex); } } Header contentDispositionHeader = response.getFirstHeader("Content-Disposition"); if (contentDispositionHeader != null) { contentDisposition = contentDispositionHeader.getValue(); } } } catch (IllegalArgumentException ex) { request.abort(); } catch (IOException ex) { request.abort(); } finally { client.close(); } if (mimeType != null) { if (mimeType.equalsIgnoreCase("text/plain") || mimeType.equalsIgnoreCase("application/octet-stream")) { String newMimeType = MimeTypeMap.getSingleton() .getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(mUri)); if (newMimeType != null) { mimeType = newMimeType; mRequest.setMimeType(newMimeType); } } String filename = URLUtil.guessFileName(mUri, contentDisposition, mimeType); mRequest.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, filename); } // Start the download DownloadManager manager = (DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE); manager.enqueue(mRequest); }
From source file:com.github.snowdream.android.apps.imageviewer.ImageViewerActivity.java
public void initData() { imageLoader = ImageLoader.getInstance(); imageUrls = new ArrayList<String>(); Intent intent = getIntent();/*from w ww . java 2s. co m*/ if (intent != null) { Bundle bundle = intent.getExtras(); if (bundle != null) { imageUrls = bundle.getStringArrayList(Extra.IMAGES); imagePosition = bundle.getInt(Extra.IMAGE_POSITION, 0); imageMode = bundle.getInt(Extra.IMAGE_MODE, 0); Log.i("The snowdream bundle path of the image is: " + imageUri); } Uri uri = (Uri) intent.getData(); if (uri != null) { imageUri = uri.getPath(); fileName = uri.getLastPathSegment(); getSupportActionBar().setSubtitle(fileName); Log.i("The path of the image is: " + imageUri); File file = new File(imageUri); imageUri = "file://" + imageUri; File dir = file.getParentFile(); if (dir != null) { FileFilter fileFilter = new FileFilter() { @Override public boolean accept(File f) { if (f != null) { String extension = MimeTypeMap .getFileExtensionFromUrl(Uri.encode(f.getAbsolutePath())); if (!TextUtils.isEmpty(extension)) { String mimeType = MimeTypeMap.getSingleton() .getMimeTypeFromExtension(extension); if (!TextUtils.isEmpty(mimeType) && mimeType.contains("image")) { return true; } } } return false; } }; File[] files = dir.listFiles(fileFilter); if (files != null && files.length > 0) { int size = files.length; for (int i = 0; i < size; i++) { imageUrls.add("file://" + files[i].getAbsolutePath()); } imagePosition = imageUrls.indexOf(imageUri); imageMode = 1; Log.i("Image Position:" + imagePosition); } } else { imageUrls.add("file://" + imageUri); imagePosition = 0; imageMode = 0; } } } else { Log.w("The intent is null!"); } }
From source file:com.android.browser.FetchUrlMimeType.java
@Override public void onPostExecute(ContentValues values) { final String mimeType = values.getAsString("Content-Type"); final String contentDisposition = values.getAsString("Content-Disposition"); if (mimeType != null) { String url = mValues.getAsString(Downloads.Impl.COLUMN_URI); if (mimeType.equalsIgnoreCase("text/plain") || mimeType.equalsIgnoreCase("application/octet-stream")) { String newMimeType = MimeTypeMap.getSingleton() .getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(url)); if (newMimeType != null) { mValues.put(Downloads.Impl.COLUMN_MIME_TYPE, newMimeType); }//from w ww . j a v a 2s . c om } String filename = URLUtil.guessFileName(url, contentDisposition, mimeType); mValues.put(Downloads.Impl.COLUMN_FILE_NAME_HINT, filename); } // Start the download final Uri contentUri = mActivity.getContentResolver().insert(Downloads.Impl.CONTENT_URI, mValues); }
From source file:com.apptentive.android.sdk.model.FileMessage.java
public boolean createStoredFile(Context context, String uriString) { Uri uri = Uri.parse(uriString);/*from w w w . j a v a2 s. com*/ ContentResolver resolver = context.getContentResolver(); String mimeType = resolver.getType(uri); MimeTypeMap mime = MimeTypeMap.getSingleton(); String extension = mime.getExtensionFromMimeType(mimeType); // If we can't get the mime type from the uri, try getting it from the extension. if (extension == null) { extension = MimeTypeMap.getFileExtensionFromUrl(uriString); } if (mimeType == null && extension != null) { mimeType = mime.getMimeTypeFromExtension(extension); } setFileName(uri.getLastPathSegment() + "." + extension); setMimeType(mimeType); InputStream is = null; try { is = new BufferedInputStream(context.getContentResolver().openInputStream(uri)); return createStoredFile(context, is, mimeType); } catch (FileNotFoundException e) { Log.e("File not found while storing file.", e); } catch (IOException e) { Log.a("Error storing image.", e); } finally { Util.ensureClosed(is); } return false; }