List of usage examples for android.webkit MimeTypeMap getSingleton
public static MimeTypeMap getSingleton()
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 ww . j a v a 2s . co 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:mil.nga.giat.mage.sdk.utils.MediaUtility.java
public static File copyMediaFromUri(Context context, Uri uri) throws IOException { InputStream is = null;/* www.j av a 2 s .co m*/ OutputStream os = null; try { ContentResolver contentResolver = context.getContentResolver(); String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); String imageFileName = "MAGE_" + timeStamp; MimeTypeMap mime = MimeTypeMap.getSingleton(); String extension = "." + mime.getExtensionFromMimeType(contentResolver.getType(uri)); File directory = context.getExternalFilesDir("media"); File file = File.createTempFile(imageFileName, /* prefix */ extension, /* suffix */ directory /* directory */ ); is = contentResolver.openInputStream(uri); os = new FileOutputStream(file); ByteStreams.copy(is, os); return file; } finally { if (is != null) { try { is.close(); } catch (IOException e) { } } if (os != null) { try { os.close(); } catch (IOException e) { } } } }
From source file:com.peppe130.fireinstaller.core.CustomFileChooser.java
File[] listFiles(String mimeType) { File[] contents = parentFolder.listFiles(new FileFilter() { @Override/*from w w w. ja v a 2 s . c om*/ public boolean accept(File file) { return getShowHiddenFiles() || !file.isHidden(); } }); List<File> results = new ArrayList<>(); if (contents != null) { MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton(); for (File fi : contents) { if (fi.isDirectory()) { results.add(fi); } else { if (fileIsMimeType(fi, mimeType, mimeTypeMap)) { results.add(fi); } } } Collections.sort(results, new FileSorter()); return results.toArray(new File[results.size()]); } return null; }
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 va 2 s. co 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) { 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:org.matrix.androidsdk.db.MXMediaWorkerTask.java
/** * Build a filename from an url/*from w w w . j a va 2 s . c o m*/ * @param Url the media url * @param mimeType the mime type; * @return the cache filename */ public static String buildFileName(String Url, String mimeType) { String name = "file_" + MXMediaWorkerTask.uniqueId(Url); if (null == mimeType) { mimeType = "image/jpeg"; } String fileExtension = MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType); // some devices don't support .jpeg files if ("jpeg".equals(fileExtension)) { fileExtension = "jpg"; } if (null != fileExtension) { name += "." + fileExtension; } return name; }
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); }/* w w w . j a v a 2s. c om*/ } 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 ww .j av 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:me.xingrz.finder.EntriesActivity.java
protected String mimeOfFile(File file) { String extension = FilenameUtils.getExtension(file.getName()); return MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension); }
From source file:com.doplgangr.secrecy.FileSystem.Vault.java
public String addFile(final Context context, final Uri uri) { String filename = uri.getLastPathSegment(); try {//from ww w.j a v a2s.c o m String realPath = getPath(context, uri); Util.log(realPath, filename); filename = new java.io.File(realPath).getName(); // If we can use real path, better use one. } catch (Exception ignored) { // Leave it. } if (!filename.contains("_thumb") && !filename.contains(".nomedia")) { ContentResolver cR = context.getContentResolver(); MimeTypeMap mime = MimeTypeMap.getSingleton(); String type = mime.getExtensionFromMimeType(cR.getType(uri)); if (type != null) filename = Base64Coder.encodeString(FilenameUtils.removeExtension(filename)) + "." + type; } InputStream is = null; OutputStream out = null; try { InputStream stream = context.getContentResolver().openInputStream(uri); java.io.File addedFile = new java.io.File(path + "/" + filename); addedFile.delete(); addedFile.createNewFile(); is = new BufferedInputStream(stream); byte buffer[] = new byte[Config.bufferSize]; int count; AES_Encryptor enc = new AES_Encryptor(key); out = new CipherOutputStream(new FileOutputStream(addedFile), enc.encryptstream()); while ((count = is.read(buffer)) != -1) out.write(buffer, 0, count); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (is != null) { is.close(); } if (out != null) { out.close(); } } catch (IOException e) { e.printStackTrace(); } } return filename; }
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 ww .jav a2 s . co 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); }